遍历多维数组[关闭]

Have the array below and need to access id but can't get it to work. Don't know how to access 3rd level of array.

Array
(
[0] => Array
    (
        [0] => Array
            (
                [id] => 1
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [id] => 2
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 3
            )

    )

[3] => Array
    (
    )

[4] => Array
    (
        [0] => Array
            (
                [id] => 5
            )

    )
}

In your example it looks as though id is always in a separate array with the key of 0;

1, Hard code:

foreach($array as $value){
    echo isset($value[0]['id']) ? $value[0]['id'] : '';
}

2, iterate though second array:

foreach($array as $key=>$value){
    if(is_array($value)){
        foreach($value as $v){
            if(isset($v['id'])){
                echo $v['id'];
            }
        }
    }
}

You can do it like this:

echo $array[0][0]['id'];

And to print them all:

foreach ($array as $arr) {
   echo $arr[0]['id'];
}

That should make it.

You do that in the following manner: $a[0][0]["id"]

First:

$multiArr = array ( [0] => array ( [0] => array ( ['id'] => 1 )));

And:

echo $multiArr[0][0]['id']; //or $multiArr['0']['0']['id']

Did you try Array[index of array 1][index of array 2][index of array 3]?

for ($i=0;$i<count($inputarray);$i++) {
    echo "ID=".$inputarray[$i][0]['id']."<br />";
}

Loops through the main array and since your sub array is always element element 0 and your 3rd level is always element id it'll pull it.

Now if your sub array contains more than one element then you'll have to double loop or reference directly still if you know its always in the same place