Jump to content

Array Inside Multidimensional Array


Go to solution Solved by NotionCommotion,

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/300767-array-inside-multidimensional-array/
Share on other sites

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'];
	}
}
  • Solution

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");
}
?>
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.