开发者

Displaying multidimensional array in php

开发者 https://www.devze.com 2022-12-17 16:17 出处:网络
I have an array like this: $tset = \"MAIN_TEST_SET\"; $gettc = \"101\"; $getbid = \"b12\"; $getresultsid = \"4587879\";

I have an array like this:

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

Arrays in PHP is confusing me.

I want to display some like this:

MAIN_TEST_SET
             101    b12     4587879
             102    b13 开发者_StackOverflow社区    4546464
             103    b14     5545465
MAIN_TEST_SET3
            201     n12     45454464

MAIN_TEST_SET4
            302     k32     36545445

How to display this?

Thanks.


print_r($users)

That will print your array out recursively in an intuitive way. See the manual: http://us2.php.net/manual/en/function.print-r.php

If you want to print it in the specific way you formatted it above you're going to have to write a custom function that uses foreach looping syntax like this:

<?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>";
?>

Your data should be composed as follows:

$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 ...


To fix the data structure you present (as Victor Nicollet mentions in his comment) you need something like this:

$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

Of course there are much more methods of creating your data structure, but this one seems/looks the most clear I can think of.

Use something like this to create a HTML table using the data structure described above.

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";

The first foreach iterates over your test sets and the second one iterates over the tests in a test set.


For a short uncustomisable output, you can use:

print_r($users)

alternatively, you can use nested loops.

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

If you are not outputting to html, replace the <br /> with "\n"

0

精彩评论

暂无评论...
验证码 换一张
取 消