codeinphp Posted February 9, 2016 Share Posted February 9, 2016 Attempting to get a value of an array that's actually inside several arrays, one being multidimensional I do believe. The array has the following structure: [catalog] => Array ( [0] => Array ( [attributes] => Array ( [book] => 20160122 ) [section] => Array ( [0] => Array ( [id] => F100 [title] => Across the Sea ) [1] => Array ( [id] => F101 [title] => Blue Water ) [2] => Array ( [id] => F102 [title] => Red Rove I have been able to get a return of the values back for each except for the id and title using the following: foreach($result as $items){ foreach($items['catalog'] as $a){ foreach($a['attributes'] as $b){ foreach($b['section'] as $c){ foreach($c['book'] as $d){ foreach($d['id'] as $e){ print_r($e); } } } } } I can't seem to get the value for the id or title. Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/300767-array-inside-multidimensional-array/ Share on other sites More sharing options...
maxxd Posted February 9, 2016 Share Posted February 9, 2016 The 'section' index is not within the 'attributes' array. Try this structure: foreach($catalog as $book){ $bookTitle = $book['attributes']['book']; foreach($book['section'] as $section){ $sxn[$section['id']] = $section['title']; } } Quote Link to comment https://forums.phpfreaks.com/topic/300767-array-inside-multidimensional-array/#findComment-1530910 Share on other sites More sharing options...
Solution NotionCommotion Posted February 9, 2016 Solution Share Posted February 9, 2016 Try the following. Also, experiment with using $key=>$value in your foreach loops. <?php $array=[ 'catalog'=>[ [ 'attributes'=>['book'=>20160122], 'section'=>[ ['id'=>'F100','title'=>'Across the Sea'], ['id'=>'F101','title'=>'Blue Water'], ['id'=>'F102','title'=>'Red Rove'] ] ], [ 'attributes'=>['book'=>20160123], 'section'=>[ ['id'=>'F103','title'=>'xAcross the Sea'], ['id'=>'F104','title'=>'xBlue Water'], ['id'=>'F105','title'=>'xRed Rove'] ] ], ]]; foreach($array['catalog'] as $catalog){ echo('Book: '.$catalog['attributes']['book']."\n"); foreach($catalog['section'] as $section){ echo($section['id'].' '.$section['title']."\n"); } echo("\n"); } ?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/300767-array-inside-multidimensional-array/#findComment-1530927 Share on other sites More sharing options...
codeinphp Posted February 14, 2016 Author Share Posted February 14, 2016 Thanks to both responses. Seeing the example helps clarify the sequence needed. Quote Link to comment https://forums.phpfreaks.com/topic/300767-array-inside-multidimensional-array/#findComment-1531107 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.