shivram D Posted February 3, 2011 Share Posted February 3, 2011 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 https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/ Share on other sites More sharing options...
joel24 Posted February 3, 2011 Share Posted February 3, 2011 what is the current array? //do this and then post the results print_r($arrayName); Link to comment https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/#findComment-1169325 Share on other sites More sharing options...
shivram D Posted February 3, 2011 Author Share Posted February 3, 2011 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 https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/#findComment-1169485 Share on other sites More sharing options...
AbraCadaver Posted February 3, 2011 Share Posted February 3, 2011 Put # code tags around the array so we can see the indentation. Link to comment https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/#findComment-1169511 Share on other sites More sharing options...
shivram D Posted February 3, 2011 Author Share Posted February 3, 2011 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 https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/#findComment-1169514 Share on other sites More sharing options...
joel24 Posted February 3, 2011 Share Posted February 3, 2011 and you want a 1D array of which values?? containing the first level topicname indexes; singapore travels, singapore entertainment... or the 2nd level topicname indexes; airlines, booking, night safari etc Link to comment https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/#findComment-1169640 Share on other sites More sharing options...
shivram D Posted February 4, 2011 Author Share Posted February 4, 2011 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 https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/#findComment-1169715 Share on other sites More sharing options...
joel24 Posted February 4, 2011 Share Posted February 4, 2011 you'll have to play around with some foreach loops Link to comment https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/#findComment-1169743 Share on other sites More sharing options...
shivram D Posted February 4, 2011 Author Share Posted February 4, 2011 thanks joel.. still a bit il try with foreach.. Link to comment https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/#findComment-1169800 Share on other sites More sharing options...
joel24 Posted February 4, 2011 Share Posted February 4, 2011 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 https://forums.phpfreaks.com/topic/226550-not-able-to-write-code-for-traversing-multidimentional-array/#findComment-1170033 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.