Godfrey Posted September 11, 2010 Share Posted September 11, 2010 Alright, hopefully my last question for this current project. I've build an array containing other arrays which each hold 5 values. I'm trying to loop through all the arrays in the main array but only pull out one of the values. What I have right now is: echo "<ol>"; for ($rw = 0; $rw < sizeof($serenwilde); $rw++) { echo "<li><b>The row number $rw</b>"; echo "<ul>"; for ($col = 0; $col < 5; $col++) { if (isset($serenwilde[$rw][$col])) { echo "<li>".$serenwilde[$rw][$col]."</li>"; } } echo "</ul>"; echo "</li>"; } echo "</ol>"; Which produces: The row number 0 value0 value1 value2 value3 value4 How can I modify my code so I still loop through all the arrays, but only pull out say value0 from each of them? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/213156-array-confusion/ Share on other sites More sharing options...
ram4nd Posted September 11, 2010 Share Posted September 11, 2010 use foreach instead of for, for an example Quote Link to comment https://forums.phpfreaks.com/topic/213156-array-confusion/#findComment-1109957 Share on other sites More sharing options...
Godfrey Posted September 11, 2010 Author Share Posted September 11, 2010 Huh... I was for some reason making that way more complex than needed. Thanks much. Quote Link to comment https://forums.phpfreaks.com/topic/213156-array-confusion/#findComment-1109964 Share on other sites More sharing options...
ram4nd Posted September 12, 2010 Share Posted September 12, 2010 Yes tough they say that for is faster than foreach, but code will be longer. Unless you put it in sperate function... Quote Link to comment https://forums.phpfreaks.com/topic/213156-array-confusion/#findComment-1110173 Share on other sites More sharing options...
sasa Posted September 12, 2010 Share Posted September 12, 2010 change for ($col = 0; $col < 5; $col++) { if (isset($serenwilde[$rw][$col])) { echo "<li>".$serenwilde[$rw][$col]."</li>"; } } to if (isset($serenwilde[$rw][0])) { echo "<li>".$serenwilde[$rw][0l]."</li>"; } Quote Link to comment https://forums.phpfreaks.com/topic/213156-array-confusion/#findComment-1110191 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.