tqla Posted August 30, 2012 Share Posted August 30, 2012 Hello, I have an array called $data that looks like this. Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) I wish to do a foreach look on this part: [1] => Array ( [0] => c [1] => d ) When I do foreach ($data as $row)... I get everything in the array. When I do foreach ($data[1] as $row)... or foreach ($data['1'] as $row)... I get nothing. What is the proper way? Thanks. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 Show us the actual code, what you're describing doesn't make much sense. You can also do a print_r() on the data to make sure it's the way you expected. Quote Link to comment Share on other sites More sharing options...
tqla Posted August 30, 2012 Author Share Posted August 30, 2012 Obviously print_r prints this: Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) My question is how do do a foreach loop on this part: [1] => Array ( [0] => c [1] => d ) Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 It's too bad I didn't write anything besides that part huh. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2012 Share Posted August 30, 2012 $myarray = array( array('a', 'b'), array('c', 'd'), array('e', 'f') ); foreach ($myarray as $row) { foreach ($row as $x) { echo "$x "; } echo '<br />'; } result a b c d e f Quote Link to comment Share on other sites More sharing options...
tqla Posted August 30, 2012 Author Share Posted August 30, 2012 Thank you Barand. Can the loop be made to echo only "c d"? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 <?php $myarray = array( array('a', 'b'), array('c', 'd'), array('e', 'f') ); foreach ($myarray[1] as $x) { echo "$x "; } ?> It works that way, which is why I said you need to post your code if when you attempt to do that, it doesn't work. Quote Link to comment Share on other sites More sharing options...
tqla Posted August 30, 2012 Author Share Posted August 30, 2012 Ah ha. Thanks jesirose. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2012 Share Posted August 30, 2012 do you mean foreach ($myarray[1] as $x) echo $x; Quote Link to comment Share on other sites More sharing options...
tqla Posted August 30, 2012 Author Share Posted August 30, 2012 Yes I do Barand. Thanks again! Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 I'm so confused...that's exactly what you said didn't work in your OP. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2012 Share Posted August 30, 2012 When I first read the OP I thought he probably had nested foreach loops both using (.... as $row) Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 Must be. Quote Link to comment Share on other sites More sharing options...
tqla Posted August 30, 2012 Author Share Posted August 30, 2012 I was using nested foreach loops. Worked fine if I wanted to echo everything. But I did not. Solution: one foreach, $data[1] as $row. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2012 Share Posted August 30, 2012 So long as you see your problem. If you have foreach ( $whatever as $row) in the outer and the inner loop, the inner "as $row" is overwriting the $row in the outer loop. Quote Link to comment Share on other sites More sharing options...
tqla Posted August 30, 2012 Author Share Posted August 30, 2012 Oh I know, I had no problem echoing the entire array as expected. Thanks Barand. 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.