如何迭代包含数组对象的多维数组?

时间:2011-11-20 22:39:55

标签: php object iteration

我在这些数组中有一个包含数组对象的多维数组。 我如何迭代特定的数组对象,例如在value2>中[1]>迭代成员数组中的所有Account_ID?

Array( 
    [value1] => text
    [value2] => Array ( 
        [0] => stdClass Object (
            [Project_Title] => Project B Test
            [members] => Array(
                [0] => stdClass Object ( [Account_ID] => 5 )
            ) 
        ) 
        [1] => stdClass Object (
            [Project_Title] => Project A Test
            [members] => Array( 
                [0] => stdClass Object ([Account_ID] => 9 ) 
                [1] => stdClass Object ([Account_ID] => 11) 
                [2] => stdClass Object ([Account_ID] => 13) 
                [3] => stdClass Object ([Account_ID] => 14) 
                [4] => stdClass Object ([Account_ID] => 15) 
                [5] => stdClass Object ([Account_ID] => 16) 
                [6] => stdClass Object ([Account_ID] => 17) 
                [7] => stdClass Object ([Account_ID] => 18) 
                [8] => stdClass Object ([Account_ID] => 19)
            )
        ) 
    )
)

2 个答案:

答案 0 :(得分:1)

基本上你可以通过完全按照你说的去做。像这样的东西会起作用!

foreach( $array['value2'][1]->members as $key => $memberObject ) {
    echo $memberObject->Account_ID ."<br />";
}

答案 1 :(得分:0)

这可能是一个基本的解决方案:

foreach ($array['value2'] as $object) {
    foreach ($object->members as $obj) { 
        echo $obj->Account_ID;
    }        
}