Php 遍历多维关联数组
我想遍历以下数组(这是我打印时的输出): 我尝试了以下代码:Php 遍历多维关联数组,php,arrays,multidimensional-array,nested-loops,Php,Arrays,Multidimensional Array,Nested Loops,我想遍历以下数组(这是我打印时的输出): 我尝试了以下代码: foreach ($result as $firtsone) { foreach ($firtsone as $key => $value) { echo $key.": ".$value; } } 这就是我得到的错误: 注意:第30行C:\xampp\htdocs\emgtsz\faulth.php中的数组到字符串转换 案例历史:阵列 请协助。问题: 这就是我得到的结果:注意:第30行的C:\xampp\htdocs
foreach ($result as $firtsone) {
foreach ($firtsone as $key => $value) {
echo $key.": ".$value;
}
}
这就是我得到的错误:
注意:第30行C:\xampp\htdocs\emgtsz\faulth.php中的数组到字符串转换
案例历史:阵列
请协助。问题: 这就是我得到的结果:注意:第30行的C:\xampp\htdocs\emgtsz\faulth.php中的数组到字符串转换CaseHistory:Array 这是因为
$value
并不总是一个字符串,它可能是一个数组
解决方案:
递归迭代数组并打印(键、值)对,如下所示:
function process_array($array){
foreach($array as $key => $value){
if(is_array($value)){
process_array($value);
}else{
echo $key . ": " . $value . "<br />";
}
}
}
process_array($result);
echo "<table border='1'>";
echo "<tr>";
echo "<th>Case Number</th>";
echo "<th>Date Created</th>";
echo "<th>Fault Type</th>";
echo "<th>status</th>";
echo "</tr>";
$arrayLength = count($result['GetRecentCasesResult']['CaseHistory']);
for($i = 0; $i < $arrayLength; ++$i){
echo "<tr>";
echo "<td>" . $result['GetRecentCasesResult']['CaseHistory'][$i]['caseNumber'] . "</td>";
echo "<td>" . $result['GetRecentCasesResult']['CaseHistory'][$i]['dateCreated'] . "</td>";
echo "<td>" . $result['GetRecentCasesResult']['CaseHistory'][$i]['faultType'] . "</td>";
echo "<td>" . $result['GetRecentCasesResult']['CaseHistory'][$i]['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
函数进程\u数组($array){
foreach($key=>$value的数组){
if(是_数组($value)){
进程_数组($value);
}否则{
echo$key.:“$value.”
;
}
}
}
处理数组($result);
编辑:
我如何将它们显示为表格而不是一个接一个
使用迭代方法,如下所示:
function process_array($array){
foreach($array as $key => $value){
if(is_array($value)){
process_array($value);
}else{
echo $key . ": " . $value . "<br />";
}
}
}
process_array($result);
echo "<table border='1'>";
echo "<tr>";
echo "<th>Case Number</th>";
echo "<th>Date Created</th>";
echo "<th>Fault Type</th>";
echo "<th>status</th>";
echo "</tr>";
$arrayLength = count($result['GetRecentCasesResult']['CaseHistory']);
for($i = 0; $i < $arrayLength; ++$i){
echo "<tr>";
echo "<td>" . $result['GetRecentCasesResult']['CaseHistory'][$i]['caseNumber'] . "</td>";
echo "<td>" . $result['GetRecentCasesResult']['CaseHistory'][$i]['dateCreated'] . "</td>";
echo "<td>" . $result['GetRecentCasesResult']['CaseHistory'][$i]['faultType'] . "</td>";
echo "<td>" . $result['GetRecentCasesResult']['CaseHistory'][$i]['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo”“;
回声“;
回显“病例编号”;
回显“创建日期”;
回声“故障类型”;
回应“状态”;
回声“;
$arrayLength=count($result['GetRecentCasesResult']['CaseShistory']);
对于($i=0;$i<$arrayLength;++$i){
回声“;
回显“$result['GetRecentCasesResult']['CaseShistory'][$i]['caseNumber']”;
回显“$result['GetRecentCasesResult']['CaseShistory'][$i]['dateCreated']”;
回显“$result['GetRecentCasesResult']['CaseShistory'][$i]['faultType']”;
回显“$result['GetRecentCasesResult']['CaseShistory'][$i]['status']”;
回声“;
}
回声“;
注意,$value
并不总是字符串,有时是数组。例如,使用is_array
进行检查。根据示例,$value始终是一个数组,因此需要再添加一个循环…@devpro我编辑了这个问题。现在查看数组。@RajdeepPaul,我如何将它们显示为表,而不是一个接一个地显示?@kya如果您想在表单元格中显示它们,那么迭代方式将是一种很好的方法。请参阅我的答案的编辑部分。