mallard Posted January 10, 2009 Share Posted January 10, 2009 I need help writing a basic foreach loop. Say I have this data: ( [file1.ext] => Array ( [0] => element0 [1] => element1 [2] => element2 [3] => element3 ) [file2.ext] => Array ( [0] => element0 [1] => element1 [2] => element2 [3] => element3 ) [file3.ext] => Array ( [0] => element0 [1] => element1 [2] => element2 [3] => element3 ) ) I want to write a foreach loop so that the output will be: file1.ext element1 element2 element3 file2.ext element1 element2 element3 file3.ext element1 element2 element3 Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/ Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 noticing the extra ( ) around the outside of the other print_r's you probably have a multidimensional array.. so you'd do like // loop through indexes of $mainArray and fill KEY to $k and VALUE to $v foreach ($mainArray as $k => $v) { // output the KEY echo $k; // loop through the value of $v, which would be an array in your example. // $va will now hold the content of each element. foreach ($v as $va) { // echo out $va with a prepended space to keep them from attaching to // eachother. echo ' '.$va; } // echo new line after the values loop, and try to find the next index of our array echo "\n"; } but note there is simpler ways of achieving this foreach ($mainArray as $k => $v) { echo $k.implode(" ",$v)."\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733855 Share on other sites More sharing options...
mallard Posted January 10, 2009 Author Share Posted January 10, 2009 Nice. Thank you for your quick response. I just plugged in the implode bit that you posted there but it did the same thing that my current foreach loop is doing. The output looks like this (I'm substituting integers to represent the key and value for the entire array for each file to keep it simple: 1 12 123 1234 12345 etc... Kind of like a pyramid. I want the output to be: 1 2 3 4 5 etc... I'm sure that there's an easy fix for this. I'm probably just making a common mistake. Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733861 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 show me your actual array please Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733863 Share on other sites More sharing options...
mallard Posted January 10, 2009 Author Share Posted January 10, 2009 Here's a small chunk of the output when I do a print_r on the array that I'm working with... Array ( [black_Crowes-Thorn_in_My_Pride-The_Southern_Harmony_and_Musical_Companion.ptb] => Array ( [0] => ptab [1] => Thorn in My Pride [2] => The Black Crowes [3] => The Southern Harmony And Musical Companion ) [Guns_N_Roses-November_Rain-Use_Your_Illusion_I.ptb] => Array ( [0] => ptab [1] => November Rain Solo [2] => Guns N' Roses [3] => Use Your Illusion I ) [Guns_N_Roses-Sweet_Child_O_Mine_Solo-Appetite_for_Distruction.ptb] => Array ( [0] => ptab [1] => Sweet Child O' Mine Solo [2] => Guns N' Roses [3] => Appetite For Distruction ) ) Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733871 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 my second or first foreach should work on that array Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733873 Share on other sites More sharing options...
mallard Posted January 10, 2009 Author Share Posted January 10, 2009 I just tried your first example and I got the same pyramid output. There must be something else somewhere in the script that's causing this. What I don't get is when I do the print_r on the array the output is normal but when I try to loop it with a foreach I get that pyramid output. Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733881 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 1 second I'm going to load up php and start testing some things for you Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733882 Share on other sites More sharing options...
mallard Posted January 10, 2009 Author Share Posted January 10, 2009 Cool, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733884 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 http://lovinglori.com/myjpg.JPG code used: <?php $arr = array(); $arr['hey'] = array("omg1","lmao1","lol1"); $arr['howdy'] = array("omg2","lmao2","lol2"); foreach ($arr as $k => $v) { echo $k; foreach ($v as $va) { echo " ".$va; } echo "\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733886 Share on other sites More sharing options...
mallard Posted January 10, 2009 Author Share Posted January 10, 2009 So then there's something else in the script that's jacking the output for me. I don't even wanna get into that. But thanks for helping me! Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733890 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 anytime buddy Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733894 Share on other sites More sharing options...
mallard Posted January 10, 2009 Author Share Posted January 10, 2009 Oh my God... I found my mistake. I was placing the loop before a closing brace "}" in the script. I moved it outside of the brace and the problem was resolved. That's all it was. I'd like to think that you helped me get there though. And you showed me a different way to bust a foreach loop. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/140251-solved-writing-a-basic-foreach-loop/#findComment-733899 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.