簡體   English   中英

如何遍歷包含數組對象的多維數組?

[英]How can I iterate over a multidimensional array containing array objects?

我有一個多維數組,這些數組中包含數組對象。 我如何遍歷特定的數組對象,例如在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)
            )
        ) 
    )
)

基本上,您可以按照自己說的去做。 這樣的事情會起作用!

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

這可能是一個基本解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM