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. Quote Link to comment 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); Quote Link to comment 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 ) ) ) Quote Link to comment 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. Quote Link to comment 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 ) Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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.. Quote Link to comment 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"; } } } Quote Link to comment 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.