Bifter Posted May 22, 2009 Share Posted May 22, 2009 Hi, how would I echo all the different values after the top level ([products]) - Basicly I need to pull all the sub levels ([product-id], [product-name], [technology-type]...etc.) from each of the [products] - there are 2 in the example below but the acual array has about 20. [products] => Array ( [0] => Array ( [product-id] => Array ( [format] => counting [value] => 1235 ) [product-name] => Array ( [format] => text [value] => Home 512k Classic ) [technology-type] => Array ( [format] => text [value] => adsl ) [capped] => Array ( [format] => yesno [value] => Y ) [service-speed] => Array ( [format] => counting [value] => 524288 ) [contention] => Array ( [format] => counting [value] => 50 ) [care-availability] => Array ( [format] => yesno [value] => Y ) [care-level] => Array ( [format] => text [value] => standard ) [realms] => Array ( [0] => Array ( [realm] => Array ( [format] => text [value] => -flipsolutions@surfdsluk ) ) ) ) [1] => Array ( [product-id] => Array ( [format] => counting [value] => 1236 ) [product-name] => Array ( [format] => text [value] => Home 1mb Classic ) [technology-type] => Array ( [format] => text [value] => adsl ) [capped] => Array ( [format] => yesno [value] => Y ) [service-speed] => Array ( [format] => counting [value] => 1048576 ) [contention] => Array ( [format] => counting [value] => 50 ) [care-availability] => Array ( [format] => yesno [value] => Y ) [care-level] => Array ( [format] => text [value] => standard ) [realms] => Array ( [0] => Array ( [realm] => Array ( [format] => text [value] => -flipsolutions@surfdsluk ) ) ) ) I guess it would be something like this in a loop -but I cannot figure it out. <?php echo ['products'] ?> I hope I have explained myself - if not please ask. Thanks for looking. Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/ Share on other sites More sharing options...
Ken2k7 Posted May 22, 2009 Share Posted May 22, 2009 Example function traversal ($arr) { foreach ($arr as $key => $val) { if (is_array($val)) traversal($val); else echo $key . ' = ' . $val; } } traversal($array['products']); Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840221 Share on other sites More sharing options...
Bifter Posted May 22, 2009 Author Share Posted May 22, 2009 Thanks for the reply - this returns: format = countingvalue = 1235format = textvalue = Home 512k Classicformat = textvalue = adslformat = yesnovalue = Yformat = countingvalue = 524288format = countingvalue = 50format = yesnovalue = Yformat = textvalue = standardformat = textvalue = but I dont think this going to work anyway, the bigger picture is that I want to get each product to return into a form with a radio button before each row where the user can select which product they want to goahead with. Then when the submit button is pressed the product id is posted to the next script - but I cannot figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840228 Share on other sites More sharing options...
Cosizzle Posted May 22, 2009 Share Posted May 22, 2009 give this a try for ($r=0; $r<sizeOf($products); $r++) { for ($c=0; $c<sizeOf($products[$r]); $c++) { echo $products[$c][$r]; } echo '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840231 Share on other sites More sharing options...
Bifter Posted May 22, 2009 Author Share Posted May 22, 2009 I can figure how this would fit in Cosizzle. My array is stored in $L Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840237 Share on other sites More sharing options...
Cosizzle Posted May 22, 2009 Share Posted May 22, 2009 your array name is 'L'? Look for 'L' in the script below and swap that out with the array name if 'L' isnt it. for ($r=0; $r<sizeOf($L); $r++) { for ($c=0; $c<sizeOf($L[$r]); $c++) { echo $L[$c][$r]; } echo '<br>'; } This should output your list within a grid format. Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840248 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 You should take another look at Ken2K7's code. Test this if you don't believe him (and me) that it does work: <?php function traversal ($arr) { foreach ($arr as $key => $value) { if(is_array($value)) { traversal($value); } else { echo $value.'<br />'; } } } $array = array( 'this', 'does', 'work', 'honestly.' ); traversal($array); ?> If it doesn't, there's something horrifically wrong with your array. Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840252 Share on other sites More sharing options...
fantomel Posted May 22, 2009 Share Posted May 22, 2009 Thanks for the reply - this returns: format = countingvalue = 1235format = textvalue = Home 512k Classicformat = textvalue = adslformat = yesnovalue = Yformat = countingvalue = 524288format = countingvalue = 50format = yesnovalue = Yformat = textvalue = standardformat = textvalue = but I dont think this going to work anyway, the bigger picture is that I want to get each product to return into a form with a radio button before each row where the user can select which product they want to goahead with. Then when the submit button is pressed the product id is posted to the next script - but I cannot figure it out. the php code they gave you works perfectly for you problem some simple modifications to add that radio button and everything should work just fine Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840257 Share on other sites More sharing options...
Bifter Posted May 22, 2009 Author Share Posted May 22, 2009 Exellent it does - dont know what I did before then! How would you get the second level returned, <?php $array = $L['products'] ['product-id']; ?> doesn't work... Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840262 Share on other sites More sharing options...
thebadbad Posted May 22, 2009 Share Posted May 22, 2009 What about <?php $products = array(); foreach ($L['products'] as $array) { $products[$array['product-id']['value']] = $array['product-name']['value']; } foreach ($products as $id => $name) { echo '<input type="radio" name="product" value="' . $id . '" /><span>' . $name . '</span><br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840269 Share on other sites More sharing options...
Bifter Posted May 22, 2009 Author Share Posted May 22, 2009 Mighty fine work all!! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/159311-solved-echoing-all-instances-in-an-array/#findComment-840274 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.