Jump to content

not able to write code for traversing multidimentional array


shivram D

Recommended Posts

this is an array returned after parsing YAMl file using spyc api.

i want to write a code to to extract value related to key topic name and put in a separate 1d array say $topicnames

 

Array ( [1] => Array ( [topicname] => singapore travels [rev] => 250

                    [0] => Array ( [25] => Array ( ) [topicname] => airlines [rev] => 24 )

                    [1] => Array ( [35] => Array ( ) [topicname] => shubham [rev] => 65 ) )

          [2] => Array ( [topicname] => singapore entertainment [rev] => 210

                    [0] => Array ( [45] => Array ( ) [topicname] => night safari[rev] => 54 )

i tried using simple for loop but not working..

pl help me out.   

hi joel..

the above is the output after executing the command print_r($data);

//$data is the array that contains yaml o/p.

// i put the array once again

 

Array ( [1] => Array ( [topicname] => singapore travels [rev] => 250

                [0] => Array ( [25] => Array ( ) [topicname] => shivram [rev] => 24 )

                [1] => Array ( [35] => Array ( ) [topicname] => shubham [rev] => 65 ) )

        [2] => Array ( [topicname] => singapore boom [rev] => 210

                [0] => Array ( [45] => Array ( ) [topicname] => appu [rev] => 54 )

                [1] => Array ( [65] => Array ( ) [topicname] => tep [rev] => 75 ) ) )       

 

the array is

 

Array ( [1] => Array ( [topicname] => singapore travels [rev] => 250
                [0] => Array ( [25] => Array ( ) [topicname] => airlines[rev] => 24 )
                [1] => Array ( [35] => Array ( ) [topicname] => booking [rev] => 65 ) )
        [2] => Array ( [topicname] => singapore entertainment [rev] => 210
                [0] => Array ( [45] => Array ( ) [topicname] => night safari [rev] => 54 )
                

hi..

i want to display the content of the whole array as a list..shown below

so thought of taking in each level in 1d array and then display it..

if u have any other solution pl tell me

 

output:

singapore travel

  • airlines
  • booking

singapore entertainment

  • night safari

 

 

you'll need some foreach inside of foreach's. something like this should give you the basic idea

i.e.

$array = array('firstLevel'=>array('2nd level'=>3));
foreach ($array as $value) {
    if (is_array($value)) {
        foreach ($value AS $key=>$secondLevel) {
            echo "key: $key, value: $secondLevel";
        }
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.