我正在尝试循环一个多维数组并且不停地撞到墙上试图找出它。 我本质上是在尝试打印所有ID值。
这是我到目前为止所做的,但它一直给我一个"数组到字符串转换"错误。
foreach ($threadsarray as $key => $threads) {
foreach ($threads as $anotherkey => $i) {
foreach($i as $id => $threadid)
echo 'key:'.$key[0]. ' AnotherKey: '.$anotherkey["threads"].' AnotherAnotherKey: '.$i["id"].' value:'.$threadid.'<br>';
}
}
我试图模仿这会做什么,但是使用&#34; For Loop&#34;
$threadsarray['threads']['0']['id'];
$threadsarray['threads']['1']['id'];
这是数组......
Array
(
[threads] => Array
(
[0] => Array
(
[archived] =>
[attachment] =>
[business_purpose] => booking_direct_thread
[id] => 178369845
[inquiry_reservation] =>
[last_message_at] => 2017-04-07T18:52:07Z
[listing] => Array
(
答案 0 :(得分:1)
这是可行的方式
foreach ($threadsarray['threads'] as $thread) {
print $thread['id'];
}
for
版
$idx = count($threadsarray['threads']);
for($i=0;$i<$idx;$i++){
print $threadsarray['threads'][$i]['id'];
}
答案 1 :(得分:0)
您得到的错误是因为您正在尝试回显一个数组。
我会这样做:
foreach($threadsarray['threads'] as $key => $thread){
echo 'key:' . $key . ' - ';
foreach($thread as $key2 => $value2){
echo 'key2:' . $key2 . ' - ';
if(is_array($value2)){
foreach($value2 as $key3 => $value3){
echo 'key3:' . $key3 . ' - value3: ' . $value3;
}
}else{
echo 'value: ' . $value2;
}
}
}
您始终可以检查整个数组结构print_r($threadsarray)
最好对任意数量的“级别”使用递归函数:
function printAll($a) {
if (!is_array($a)) {
echo $a, ' ';
return;
}
foreach($a as $k => $value) {
printAll($k);
printAll($value);
}
}
您可以将其命名为:printAll($threadsarray);