Php çok boyutlu bir dizi gösteriliyor

3 Cevap php

Ben böyle bir dizi var:

$tset = "MAIN_TEST_SET";
$gettc = "101";
$getbid = "b12";
$getresultsid = "4587879";
    $users["$tset"] = array(
                "testcase"=>"$gettc",
                "buildid"=>"$getbid",
                "resultsid"=>"$getresultsid" 
                  );

PHP Diziler beni karıştırıyor.

Ben bu gibi bazı göstermek istiyorum:

MAIN_TEST_SET
             101    b12     4587879
             102    b13     4546464
             103    b14     5545465
MAIN_TEST_SET3
            201     n12     45454464

MAIN_TEST_SET4
            302     k32     36545445

Bu nasıl görüntülenir?

Teşekkürler.

3 Cevap

print_r($users)

Bu sezgisel bir şekilde ardışık diziyi yazdıracaktır. Kılavuzuna bakınız: http://us2.php.net/manual/en/function.print-r.php

Eğer böyle foreach döngü sözdizimi kullanan bir özel bir fonksiyon yazmak zorunda gidiyoruz yukarıda biçimlendirilmiş özel bir şekilde bunu yazdırmak istiyorsanız:

<?php
echo "<table>";
foreach($users as $testSetName=>$arrayOfTestCases){
  echo "<tr><td>".$testSetName."</td></tr>";
  foreach($arrayOfTestCases as $k=>$arrayOfTestCaseFields){
    //$arrayOfTestCaseFields should be an array of test case data associated with $testSetName
    $i = 0;
    foreach($arrayOfTestCaseFields as $fieldName => $fieldValue){
      //$fieldValue is a field of a testcase $arrayOfTestCaseFields
      if($i == 0){
        //inject a blank table cell at the beginning of each test case row.
        echo "<tr><td>&nbsp;</td>";          
        $i++;
      }        
      echo "<td>".$fieldValue."</td>";            
    }
    echo "</tr>";
  }
}
echo "</table>";
?>

Aşağıdaki gibi veri oluşmalıdır:

$tset = "MAIN_TEST_SET"; 
$gettc = "101"; 
$getbid = "b12"; 
$getresultsid = "4587879"; 
$users[$tset] = array();
$users[$tset][] = array( "testcase"=>"$gettc", 
                         "buildid"=>"$getbid", 
                         "resultsid"=>"$getresultsid"  
                  ); 
   $users[$tset][] = ... and so forth ...

(Victor Nicollet yaptığı açıklamada bahseder gibi) mevcut veri yapısını düzeltmek için böyle bir şey gerekir:

$users = array();   // Create an empty array for the users? (Maybe testsets is a better name?)

$testset = array(); // Create an empty array for the first testset
// Add the test details to the testset (array_push adds an item (an array containing the results in this case) to the end of the array)
array_push($testset, array("testcase"=>"101", "buildid"=>"b12", "resultsid" => "4587879"));
array_push($testset, array("testcase"=>"102", "buildid"=>"b13", "resultsid" => "4546464"));
// etc

// Add the testset array to the users array with a named key
$users['MAIN_TEST_SET'] = $testset;

// Repeat for the other testsets
$testset = array(); // Create an empty array for the second testset
// etc

Tabii ki sizin veri yapısını oluşturmaya çok daha fazla yöntem vardır, ama bu bir Aklıma en net görünüyor / görünüyor.

Yukarıda açıklanan veri yapısını kullanarak bir HTML tablosu oluşturmak için böyle bir şey kullanın.

echo "<table>\n";
foreach($users as $tsetname => $tset)
{
    echo '<tr><td colspan="3">'.$tsetname.'</td></tr>\n';
    foreach($tset as $test)
        echo "<tr><td>".$test['testcase']."</td><td>".$test['buildid']."</td><td>".$test['resultsid']."</td></tr>\n";
}
echo "</table>\n";

Test setleri ve test seti testleri üzerinde ikinci bir dolaşır üzerinde ilk foreach dolaşır.

Kısa bir uncustomisable çıktı için kullanabilirsiniz:

print_r($users)

alternatif olarak, iç içe döngüler kullanabilirsiniz.

foreach($users as $key => $user) {
    echo $key . "<br />"
    echo $user["testcase"] . " " . $user["buildid"] . " " . $user["resultsid"];
}

Eğer html çıktısı değilseniz, "\ n" ile <br /> olarak değiştirin