tready29483 Posted November 16, 2007 Share Posted November 16, 2007 Ok, I parse through a xml file and create an array of the data. I now need to loop through all the nested arrays but I'm unsure of how to do this. The Array: Array ( [0] => Array ( [name] => BOARD [attrs] => Array ( [CATEGORY] => total [FILTER] => [sTAT] => total_xp [PAGE] => 1 [ALL_PAGES] => 400 ) [child] => Array ( [0] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Supreme Commander [TOTAL_ACCURACY] => 37.1042 [TOTAL_DEATHS] => 7796 [TOTAL_KILLS] => 32450 [COUNTRY] => United States [uSERNAME] => MechanicaL [RANK] => 1 [TOTAL_XP] => 136314 [uSER_ID] => 25398 ) ) [1] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Supreme Commander [TOTAL_ACCURACY] => 34.5358 [TOTAL_DEATHS] => 14760 [TOTAL_KILLS] => 19125 [COUNTRY] => Australia [uSERNAME] => Kazahel [RANK] => 2 [TOTAL_XP] => 133050 [uSER_ID] => 2386 ) ) [2] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Brigadier General [TOTAL_ACCURACY] => 23.7006 [TOTAL_DEATHS] => 6342 [TOTAL_KILLS] => 13551 [COUNTRY] => Unknown [uSERNAME] => Elektromode [RANK] => 3 [TOTAL_XP] => 131908 [uSER_ID] => 6933 ) ) [3] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => General [TOTAL_ACCURACY] => 34.7196 [TOTAL_DEATHS] => 6286 [TOTAL_KILLS] => 26430 [COUNTRY] => United States [uSERNAME] => DomMafia [RANK] => 4 [TOTAL_XP] => 126922 [uSER_ID] => 21597 ) ) [4] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Supreme Commander [TOTAL_ACCURACY] => 31.6598 [TOTAL_DEATHS] => 8380 [TOTAL_KILLS] => 14641 [COUNTRY] => Netherlands [uSERNAME] => VanSlater [RANK] => 5 [TOTAL_XP] => 121213 [uSER_ID] => 5441 ) ) [5] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => General [TOTAL_ACCURACY] => 31.008 [TOTAL_DEATHS] => 14387 [TOTAL_KILLS] => 22521 [COUNTRY] => Unknown [uSERNAME] => 100DM [RANK] => 6 [TOTAL_XP] => 120624 [uSER_ID] => 465 ) ) [6] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Supreme Commander [TOTAL_ACCURACY] => 33.1214 [TOTAL_DEATHS] => 10733 [TOTAL_KILLS] => 13116 [COUNTRY] => Germany [uSERNAME] => RaLLe [RANK] => 7 [TOTAL_XP] => 118551 [uSER_ID] => 2140 ) ) [7] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => General [TOTAL_ACCURACY] => 31.2117 [TOTAL_DEATHS] => 9432 [TOTAL_KILLS] => 26060 [COUNTRY] => Germany [uSERNAME] => aRiz [RANK] => 8 [TOTAL_XP] => 114948 [uSER_ID] => 1258 ) ) [8] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Field Marshal [TOTAL_ACCURACY] => 25.7771 [TOTAL_DEATHS] => 16830 [TOTAL_KILLS] => 15526 [COUNTRY] => Unknown [uSERNAME] => DressToKill [RANK] => 9 [TOTAL_XP] => 113466 [uSER_ID] => 12786 ) ) [9] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Commander General [TOTAL_ACCURACY] => 29.5076 [TOTAL_DEATHS] => 12975 [TOTAL_KILLS] => 23317 [COUNTRY] => Unknown [uSERNAME] => Zipoui [RANK] => 10 [TOTAL_XP] => 111853 [uSER_ID] => 39062 ) ) [10] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Lieutenant General [TOTAL_ACCURACY] => 29.8177 [TOTAL_DEATHS] => 9437 [TOTAL_KILLS] => 14636 [COUNTRY] => Unknown [uSERNAME] => BBBYurifol [RANK] => 11 [TOTAL_XP] => 107715 [uSER_ID] => 8266 ) ) [11] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Colonel [TOTAL_ACCURACY] => 25.2158 [TOTAL_DEATHS] => 7822 [TOTAL_KILLS] => 13438 [COUNTRY] => United States [uSERNAME] => M2MG [RANK] => 12 [TOTAL_XP] => 106766 [uSER_ID] => 29470 ) ) [12] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Lieutenant General [TOTAL_ACCURACY] => 26.925 [TOTAL_DEATHS] => 12850 [TOTAL_KILLS] => 14459 [COUNTRY] => Unknown [uSERNAME] => frizlefry [RANK] => 13 [TOTAL_XP] => 102323 [uSER_ID] => 23736 ) ) [13] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Field Marshal [TOTAL_ACCURACY] => 32.2771 [TOTAL_DEATHS] => 12636 [TOTAL_KILLS] => 20110 [COUNTRY] => Unknown [uSERNAME] => MixoMaToZ [RANK] => 14 [TOTAL_XP] => 100553 [uSER_ID] => 7041 ) ) [14] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Colonel [TOTAL_ACCURACY] => 35.3787 [TOTAL_DEATHS] => 9431 [TOTAL_KILLS] => 22796 [COUNTRY] => United States [uSERNAME] => Sgt.Hardup [RANK] => 15 [TOTAL_XP] => 96971 [uSER_ID] => 21688 ) ) [15] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Group Commander [TOTAL_ACCURACY] => 27.8226 [TOTAL_DEATHS] => 6271 [TOTAL_KILLS] => 12102 [COUNTRY] => Germany [uSERNAME] => cr4y75 [RANK] => 16 [TOTAL_XP] => 96666 [uSER_ID] => 7757 ) ) [16] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Lieutenant General [TOTAL_ACCURACY] => 28.8619 [TOTAL_DEATHS] => 16793 [TOTAL_KILLS] => 18300 [COUNTRY] => Unknown [uSERNAME] => Tanzverbot [RANK] => 17 [TOTAL_XP] => 96432 [uSER_ID] => 658 ) ) [17] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Field Marshal [TOTAL_ACCURACY] => 30.7003 [TOTAL_DEATHS] => 8085 [TOTAL_KILLS] => 16006 [COUNTRY] => United Kingdom [uSERNAME] => DyLaN [RANK] => 18 [TOTAL_XP] => 95060 [uSER_ID] => 6304 ) ) [18] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => General [TOTAL_ACCURACY] => 29.631 [TOTAL_DEATHS] => 8697 [TOTAL_KILLS] => 14547 [COUNTRY] => France [uSERNAME] => X-Black [RANK] => 19 [TOTAL_XP] => 94398 [uSER_ID] => 43726 ) ) [19] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Brigadier [TOTAL_ACCURACY] => 23.8554 [TOTAL_DEATHS] => 13515 [TOTAL_KILLS] => 12251 [COUNTRY] => United States [uSERNAME] => Natural-Causes [RANK] => 20 [TOTAL_XP] => 92535 [uSER_ID] => 22438 ) ) [20] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => General [TOTAL_ACCURACY] => 40.0914 [TOTAL_DEATHS] => 11263 [TOTAL_KILLS] => 13268 [COUNTRY] => Germany [uSERNAME] => eX2tremiousU [RANK] => 21 [TOTAL_XP] => 92528 [uSER_ID] => 3107 ) ) [21] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Major General [TOTAL_ACCURACY] => 27.2189 [TOTAL_DEATHS] => 11912 [TOTAL_KILLS] => 10992 [COUNTRY] => Germany [uSERNAME] => Achilles33 [RANK] => 22 [TOTAL_XP] => 91745 [uSER_ID] => 8019 ) ) [22] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Commander General [TOTAL_ACCURACY] => 32.2195 [TOTAL_DEATHS] => 9057 [TOTAL_KILLS] => 14369 [COUNTRY] => United Kingdom [uSERNAME] => Silent.Jay [RANK] => 23 [TOTAL_XP] => 91594 [uSER_ID] => 4921 ) ) [23] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Commander General [TOTAL_ACCURACY] => 36.8538 [TOTAL_DEATHS] => 7466 [TOTAL_KILLS] => 14500 [COUNTRY] => Germany [uSERNAME] => zyRot [RANK] => 24 [TOTAL_XP] => 91265 [uSER_ID] => 34702 ) ) [24] => Array ( [name] => PLAYER [attrs] => Array ( [MILITARY_RANK] => Field Marshal [TOTAL_ACCURACY] => 37.7674 [TOTAL_DEATHS] => 6106 [TOTAL_KILLS] => 20436 [COUNTRY] => Netherlands [uSERNAME] => Pure [RANK] => 25 [TOTAL_XP] => 89957 [uSER_ID] => 11080 ) ) ) ) ) Function I use: function fnGetTotals($arrData) { foreach ($arrData[0]['child'] as $strKey =>$arrValues) { if ($arrValues['name']=='PLAYER') { return $arrValues['attrs']; } } } var_dump of results: array(9) { ["MILITARY_RANK"]=> string(17) "Supreme Commander" ["TOTAL_ACCURACY"]=> string(7) "37.1042" ["TOTAL_DEATHS"]=> string(4) "7796" ["TOTAL_KILLS"]=> string(5) "32450" ["COUNTRY"]=> string(13) "United States" ["USERNAME"]=> string(10) "MechanicaL" ["RANK"]=> string(1) "1" ["TOTAL_XP"]=> string(6) "136314" ["USER_ID"]=> string(5) "25398" } The problem is it only returns the first child node. I need a way to return all child nodes and its attributes. Can anyone help? Quote Link to comment Share on other sites More sharing options...
premiso Posted November 16, 2007 Share Posted November 16, 2007 Edit: My bad recursion is not needed here. function fnGetTotals($arrData) { $arrValues_list = array(); foreach ($arrData[0]['child'] as $strKey =>$arrValues) { if ($arrValues['name']=='PLAYER') { $arrValues_list[]['attrs'] = $arrValues['attrs']; } } return $arrValues_list; } Unsure if it works, but I think that is what you are looking for. 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.