用php显示多维数组

用php显示多维数组,php,multidimensional-array,Php,Multidimensional Array,我有这样一个数组: $tset = "MAIN_TEST_SET"; $gettc = "101"; $getbid = "b12"; $getresultsid = "4587879"; $users["$tset"] = array( "testcase"=>"$gettc", "buildid"=>"$getbid", "resultsid"=>"$getresults

我有这样一个数组:

$tset = "MAIN_TEST_SET";
$gettc = "101";
$getbid = "b12";
$getresultsid = "4587879";
    $users["$tset"] = array(
                "testcase"=>"$gettc",
                "buildid"=>"$getbid",
                "resultsid"=>"$getresultsid" 
                  );
MAIN_TEST_SET
             101    b12     4587879
             102    b13     4546464
             103    b14     5545465
MAIN_TEST_SET3
            201     n12     45454464

MAIN_TEST_SET4
            302     k32     36545445
<?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>";
?>
$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
PHP中的数组让我感到困惑

我想展示一些像这样的:

$tset = "MAIN_TEST_SET";
$gettc = "101";
$getbid = "b12";
$getresultsid = "4587879";
    $users["$tset"] = array(
                "testcase"=>"$gettc",
                "buildid"=>"$getbid",
                "resultsid"=>"$getresultsid" 
                  );
MAIN_TEST_SET
             101    b12     4587879
             102    b13     4546464
             103    b14     5545465
MAIN_TEST_SET3
            201     n12     45454464

MAIN_TEST_SET4
            302     k32     36545445
<?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>";
?>
$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
如何显示这个

谢谢

print_r($users)
这将以直观的方式递归输出数组。请参阅手册:

如果要以上面格式化的特定方式打印,必须编写一个自定义函数,该函数使用foreach循环语法,如下所示:

$tset = "MAIN_TEST_SET";
$gettc = "101";
$getbid = "b12";
$getresultsid = "4587879";
    $users["$tset"] = array(
                "testcase"=>"$gettc",
                "buildid"=>"$getbid",
                "resultsid"=>"$getresultsid" 
                  );
MAIN_TEST_SET
             101    b12     4587879
             102    b13     4546464
             103    b14     5545465
MAIN_TEST_SET3
            201     n12     45454464

MAIN_TEST_SET4
            302     k32     36545445
<?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>";
?>
$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

要修复您当前的数据结构(正如Victor Nicolet在其评论中提到的),您需要以下内容:

$tset = "MAIN_TEST_SET";
$gettc = "101";
$getbid = "b12";
$getresultsid = "4587879";
    $users["$tset"] = array(
                "testcase"=>"$gettc",
                "buildid"=>"$getbid",
                "resultsid"=>"$getresultsid" 
                  );
MAIN_TEST_SET
             101    b12     4587879
             102    b13     4546464
             103    b14     5545465
MAIN_TEST_SET3
            201     n12     45454464

MAIN_TEST_SET4
            302     k32     36545445
<?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>";
?>
$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
当然,创建数据结构的方法还有很多,但这似乎是我能想到的最清晰的方法

使用类似的方法使用上述数据结构创建HTML表

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";
echo“\n”;
foreach($tsetname=>$tset的用户)
{
回显'.$tsetname'.\n';
foreach($tset作为$test)
回显“$test['testcase']”、$test['buildid']”、$test['resultsid']”、“\n”;
}
回音“\n”;

第一个foreach迭代测试集,第二个foreach迭代测试集中的测试。

对于短的不可计算输出,您可以使用:

print_r($users)
或者,可以使用嵌套循环

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

如果您没有输出到html,请将

替换为“\n”

我想在html的表格中显示它您的html表格不显示OP给出的示例表格的内容。它仅显示测试集中的一行测试数据。您的
$users
数组将一个键(例如
MAIN_test_set
)映射到包含三个值(例如
102、b12、4587879
)的单个数组。因此,您不可能在输出中有三行用于
MAIN\u TEST\u SET
,因为再次分配给键将删除以前的值……是的,您是对的。我丢失了一些数据。您能告诉我如何以我想要的格式存储吗?$users[$tset]应该设置为array()。然后,对于每个测试用例,您要执行$users[$tset][]=array(“testcase”=>…);见下文。