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.   

Link to comment
Share on other sites

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 ) ) )       

 

Link to comment
Share on other sites

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 )
                

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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";
        }
    }
}

Link to comment
Share on other sites

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.