1

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     4546464
             103    b14     5545465
MAIN_TEST_SET3
            201     n12     45454464

MAIN_TEST_SET4
            302     k32     36545445

How to display this?

Thanks.

3
  • Your $users array maps a key (such as MAIN_TEST_SET) to a single array of three values (such as 102, b12, 4587879). It is therefore impossible for you to have three lines for MAIN_TEST_SET in your output, as assigning again to your key will erase the previous value... Commented Jan 22, 2010 at 18:51
  • yes, you are right. I am missing some data. Can you please let me know how to store in the format I want? Commented Jan 22, 2010 at 19:06
  • $users[$tset] should be set to array(). Then for each test case you want to do $users[$tset][] = array("testcase"=>...); see below. Commented Jan 22, 2010 at 19:41

3 Answers 3

2
print_r($users)

That will print your array out recursively in an intuitive way. See the manual: https://www.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 ...
Sign up to request clarification or add additional context in comments.

2 Comments

I want to display it in a table in HTML
Your HTML table does not show the contents of the example table the OP gave. It only shows one row of test data within a test set.
1

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.

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.