here is the culprit himself

array(10) { [0]=> array(1) { [0]=> object(stdClass)#110 (7) { ["id"]=> string(5) "27741" ["type"]=> string(8) "KPPeople" ["nameRU"]=> string(30) "Π”ΠΆΠ΅ΠΉ Π”ΠΆΠ΅ΠΉ Абрамс" ["nameEN"]=> string(11) "JJ Abrams" ["posterURL"]=> string(29) "actor_iphone/iphone_27741.jpg" ["professionText"]=> string(18) "РСТиссСры" ["professionKey"]=> string(8) "director" } } [1]=> array(127) { [0]=> object(stdClass)#108 (8) { ["id"]=> string(7) "2196854" ["type"]=> string(8) "KPPeople" ["nameRU"]=> string(21) "Π”ΠΆΠΎΠ½ Π‘ΠΎΠΉΠ΅Π³Π°" ["nameEN"]=> string(11) "John Boyega" ["description"]=> string(4) "Finn" ["posterURL"]=> string(31) "actor_iphone/iphone_2196854.jpg" ["professionText"]=> string(12) "АктСры" ["professionKey"]=> string(5) "actor" } [1]=> object(stdClass)#111 (8) { ["id"]=> string(7) "3016071" ["type"]=> string(8) "KPPeople" ["nameRU"]=> string(21) "Дэйзи Π ΠΈΠ΄Π»ΠΈ" ["nameEN"]=> string(12) "Daisy Ridley" ["description"]=> string(3) "Rey" ["posterURL"]=> string(31) "actor_iphone/iphone_3016071.jpg" ["professionText"]=> string(12) "АктСры" ["professionKey"]=> string(5) "actor" }... } [2]=> array(14) { [0]=> object(stdClass)#237 (7) { ["id"]=> string(5) "27741" ["type"]=> string(8) "KPPeople" ["nameRU"]=> string(30) "Π”ΠΆΠ΅ΠΉ Π”ΠΆΠ΅ΠΉ Абрамс" ["nameEN"]=> string(11) "JJ Abrams" ...} [3]=> array(1) { [0]=> object(stdClass)#251 (7) { ["id"]=> string(7) "2469864" ["type"]=> string(8) "KPPeople" ["nameRU"]=> string(33) "Π“Π΅ΠΎΡ€Π³ΠΈΠΉ ДаниСлянц" ["nameEN"]=> string(0) "" ["posterURL"]=> string(31) "actor_iphone/iphone_2469864.jpg" ["professionText"]=> string(33) "РСТиссСры дубляТа" ["professionKey"]=> string(14) "voice_director" } }... 

I need to get id, nameRU, posterURL and professionKey and the number of arrays of the first level and the second as you can see can be from 0 to infinity

  • and what the foreach enclosed in foreach doesn't suit? You have the same object at the end, so in the second cycle you need to treat it as an object, respectively - korytoff

2 answers 2

The nested cycle here, in my opinion, is quite appropriate:

 $data = array(/* ... */); // ваш исходный массив for( $i=0; $i<count( $data); $i++) { $inner = $data[ $i]; if( !is_array( $inner) || count( $inner)===0) continue; for( $j=0; $j<count( $inner); $j++) { $item = $inner[ $j]; printf( '<li>%d "%s"<br><img src="%s" alt=""><br>(%s)</li>', $item->id, $item->nameRU, $item->posterURL, $item->professionKey ); } } 
     $source = array(....); $result = array(); array_walk_recursive($source, function ($item, $key) use (&$result) { if(isset($item['id']){ $result[]=array( 'id'=>$item['id'], 'nameRU'=>$item['nameRU'], 'posterURL'=>$item['posterURL'], 'professionKey'=>$item['professionKey'] ); } });