natasha_thomas Posted March 1, 2011 Share Posted March 1, 2011 Folks, I have an Array as shown in the below Structre (An array has multiple parents and childs) Array $productlist Array { 1 => A 2 => B 3 => C } Array { 1 => A 2 => B 3 => C 4 => D 5 => E } Array { 1 => A 2 => B 3 => C 4 => D 5 => E 6 => E 7 => E 8 => E } $productlist Array can have N numbers of Parents array as shown in above example, (above its 3 it can be anything). All i want is, i want to print_r only the last Parent rray and all its child elements. So in this case it will be: Array { 1 => A 2 => B 3 => C 4 => D 5 => E 6 => E 7 => E 8 => E } HOw can i achieve this with PHP, i tried even count() but its not working not even array_pop(). Can someone help me with this? Regards, Natasha Quote Link to comment Share on other sites More sharing options...
mattal999 Posted March 1, 2011 Share Posted March 1, 2011 Well, you could just do this: $array = array(array("1" => "A"), array("2" => "B", "3" => "C"), array("4" => "D")); foreach($array as $child) $temparray = $child; print_r($temparray); // Should contain the last element in $array. // Output: array { "4" => "D" } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted March 1, 2011 Share Posted March 1, 2011 print_r( end($productlist) ); Quote Link to comment Share on other sites More sharing options...
flolam Posted March 1, 2011 Share Posted March 1, 2011 <?php $array = array(array("1" => "A"), array("2" => "B", "3" => "C"), array("4" => "D")); print_r(end($array)); ?> --edit-- too slow ... Quote Link to comment Share on other sites More sharing options...
natasha_thomas Posted March 1, 2011 Author Share Posted March 1, 2011 Well, you could just do this: $array = array(array("1" => "A"), array("2" => "B", "3" => "C"), array("4" => "D")); foreach($array as $child) $temparray = $child; print_r($temparray); // Should contain the last element in $array. // Output: array { "4" => "D" } I did this, but the output is the Last child node of each Parent Array, Its not what i am looking for. All i want is, the code should give me All the Childs of the last parent Node. haev a look at my Example above, so the desired output will be: Array { 1 => A 2 => B 3 => C 4 => D 5 => E 6 => E 7 => E 8 => E } Make Sense? Cheers NT Quote Link to comment Share on other sites More sharing options...
mattal999 Posted March 1, 2011 Share Posted March 1, 2011 print_r( end($productlist) ); Just goes to show that there's always a more efficient way to do things in PHP I did this, but the output is the Last child node of each Parent Array, Its not what i am looking for. Take a look ad AbraCadaver's code above. That should work as you want it to, although so should mine... Quote Link to comment Share on other sites More sharing options...
natasha_thomas Posted March 1, 2011 Author Share Posted March 1, 2011 print_r( end($productlist) ); Just goes to show that there's always a more efficient way to do things in PHP I did this, but the output is the Last child node of each Parent Array, Its not what i am looking for. Take a look ad AbraCadaver's code above. That should work as you want it to, although so should mine... Many thanks, the one worked. So the output is all CHild nodes of LAst parent. Another complication is: Echo Child Nodes of Last parent node has multiple words in it. So consider $lastArry as the last parent node and here are its Child: child 1 : Paintball mask child 2 : paintball helmet child 3 : sniper ball What i want is to make a new array where each word of Each Child node in Last Parent node becomes a Child of New Array Like: NewArray { 1 = > paintball 2 => mask 3 => paintball 4 => helmet 5 =>sniper 6 => ball } How can i achieve this? I have used Explode() to do this, but could not achieve this. Cheers Natasha Quote Link to comment Share on other sites More sharing options...
mattal999 Posted March 1, 2011 Share Posted March 1, 2011 $array = array("Paintball Mask", "Paintball Helmet", "Sniper Ball"); $newarray = array(); foreach($array as $child) { $temp = explode(" ", $child); foreach($temp as $tchild) { $newarray[] = $tchild; } } print_r($newarray); Quote Link to comment Share on other sites More sharing options...
natasha_thomas Posted March 1, 2011 Author Share Posted March 1, 2011 $array = array("Paintball Mask", "Paintball Helmet", "Sniper Ball"); $newarray = array(); foreach($array as $child) { $temp = explode(" ", $child); foreach($temp as $tchild) { $newarray[] = $tchild; } } print_r($newarray); Thanks Mattle, but this code is not working. Once again, below is the String i got after imploding all the Children of last Array: airganix na50 air purifier nscessity: hepa air purifier heaven fresh hf 200 ionic air purifier [baby product] vicks v-9071 hepa air purifier heaven fresh hf 210uv ioninc air purifier w/fan & uv lamp [baby product] heaven fresh hf 100 air purifier [baby product] lloydspharmacy ionising air purifier nscessity: tio2 uv plasma air purifier cleanaer air purifier - removes allergens, dust & smoke from the air you breathe pure allergy cartridge & batteries for cleanaer air purifier Now all i want is, to have Each word of this String in an Array.... I tried to use explode(), but i could not make that work.. Did i miss something? Cheers NT Quote Link to comment Share on other sites More sharing options...
mattal999 Posted March 1, 2011 Share Posted March 1, 2011 You don't need to implode the array. You have what you want to get in the variable $newarray. Using your example it would contain: Array ( 0 => "airganix", 1 => "na50", 2 => "air", 3 => ... ) Or am I missing something? Quote Link to comment Share on other sites More sharing options...
MikeDean89 Posted March 1, 2011 Share Posted March 1, 2011 If you could post the code you currently have (perhaps only the relevant parts), it should hopefully stop the second guessing that's going on. Mike. Quote Link to comment Share on other sites More sharing options...
natasha_thomas Posted March 1, 2011 Author Share Posted March 1, 2011 If you could post the code you currently have (perhaps only the relevant parts), it should hopefully stop the second guessing that's going on. Mike. $prodname[] = strtolower($product->name); $prodlast = end($prodname); echo $prodlast; Gave me airganix na50 air purifier nscessity: hepa air purifier heaven fresh hf 200 ionic air purifier [baby product] vicks v-9071 hepa air purifier heaven fresh hf 210uv ioninc air purifier w/fan & uv lamp [baby product] heaven fresh hf 100 air purifier [baby product] lloydspharmacy ionising air purifier nscessity: tio2 uv plasma air purifier cleanaer air purifier - removes allergens, dust & smoke from the air you breathe pure allergy cartridge & batteries for cleanaer air purifier But funny thing is, this output itself is an array like: After doing echo '<pre>'; echo $prodlast; echo '</pre>'; Output was: bliss aroma diffuser aqua air purifier humidifier air freshener essential oil diffuser scent... nirvana aroma diffuser air purifier humidifier air freshener essential oil diffuser scent diffuser bliss aroma diffuser amber air purifier humidifier air freshener essential oil diffuser scent... enso aroma diffuser air purifier humidifier air freshener essential oil diffuser scent diffuser blue premiair hepa filter air purifierfresh air globe purifier ioniser & humidifier colour changing led ligh new! ideal birthday gift set!! enso aroma diffuser air purifier humidifier air freshener essential... sharp kc860ekw air purifier with humidifier - whiteaeromag non-electric evaporative air humidifier & purifier (50 cm, cca. 0.6 litre All i want is, each word to be the child of a New Array. Natasha Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted March 1, 2011 Share Posted March 1, 2011 $result = array(); foreach($prodlast as $child) { $result = array_merge($result, explode(' ', $child)); } print_r($result); Quote Link to comment Share on other sites More sharing options...
natasha_thomas Posted March 1, 2011 Author Share Posted March 1, 2011 $result = array(); foreach($prodlast as $child) { $result = array_merge($result, explode(' ', $child)); } print_r($result); I edited my code as you suggested: $prodlast = end($prodname); $result = array(); foreach($prodlast as $child) { $result = array_merge($result, explode(' ', $child)); } echo '<pre>'; print_r($result); echo '</pre>'; Output: Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Quote Link to comment Share on other sites More sharing options...
mattal999 Posted March 1, 2011 Share Posted March 1, 2011 Give my code a whack: $prodname[] = strtolower($product->name); $prodlast = end($prodname); $prodtags = array(); foreach($prodlast as $child) { $temp = explode(" ", $child); foreach($temp as $tchild) { $prodtags[] = $tchild; } } print_r($prodtags); Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted March 1, 2011 Share Posted March 1, 2011 Well, I am confused about all the variations of your data and how it seems to change. This works: $prodlast[] = 'Paintball mask'; $prodlast[] = 'paintball helmet'; $prodlast[] = 'sniper ball'; $result = array(); foreach($prodlast as $child) { $result = array_merge($result, explode(' ', $child)); } print_r($result); It yields: Array ( [0] => Paintball [1] => mask [2] => paintball [3] => helmet [4] => sniper [5] => ball ) Quote Link to comment Share on other sites More sharing options...
mattal999 Posted March 1, 2011 Share Posted March 1, 2011 Well, I am confused about all the variations of your data and how it seems to change. This works: $prodlast[] = 'Paintball mask'; $prodlast[] = 'paintball helmet'; $prodlast[] = 'sniper ball'; $result = array(); foreach($prodlast as $child) { $result = array_merge($result, explode(' ', $child)); } print_r($result); It yields: Array ( [0] => Paintball [1] => mask [2] => paintball [3] => helmet [4] => sniper [5] => ball ) Just use this. Much better than my solution. Quote Link to comment Share on other sites More sharing options...
natasha_thomas Posted March 1, 2011 Author Share Posted March 1, 2011 Give my code a whack: $prodname[] = strtolower($product->name); $prodlast = end($prodname); $prodtags = array(); foreach($prodlast as $child) { $temp = explode(" ", $child); foreach($temp as $tchild) { $prodtags[] = $tchild; } } print_r($prodtags); output is: Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) :'( 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.