proggR Posted July 3, 2008 Share Posted July 3, 2008 Is there any way to access each individual array element individually with something as simple as a loop? I'm actually using a two dimensional array created with array_chunk($input, 5); From there I want to have one loop for the first index and a second embedded loop for the second index So I want: for ($i = 0;$i<=$max;$i++){ for ($j = 0;$j<=5;$j++){ echo $array[$i][$j]; } } but when I do that it just prints array[1], array[2] etc. Any help? I've read some about for ($var as $varname) or something like that but forget. can i just use a simple loop like i have? Thanks in advance. I haven't worked on this for a while so I thought I'd ask. Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 3, 2008 Share Posted July 3, 2008 Like this? foreach ($array as $key1) { foreach($array[$key1] as $key2 => $value) echo $value; } Quote Link to comment Share on other sites More sharing options...
proggR Posted July 3, 2008 Author Share Posted July 3, 2008 yah thats what i've seen before. could someone explain the logic of it? i'm not sure I fully understand that. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 3, 2008 Share Posted July 3, 2008 Best place is the manual. If its very simple syntax. Quote Link to comment Share on other sites More sharing options...
teynon Posted July 3, 2008 Share Posted July 3, 2008 foreach ($array as $key1) ------------------------- This loop basically does this: $arrayLength=count($array); for ($x=0; $x<$arrayLength; $x++) { echo $array[$x]; } Having foreach ($array as $value) { foreach ($value as $subvalue) { } } simply does the same thing except for multidimensional arrays. That is only one leve down. So here is the code and what it would do: $array[fun][test]=5; $array[fun][test2]=6; $array[morefun][test]=1; foreach ($array as $key) { foreach ($key as $value) { echo $key.">".$value."<br />"; } } Output: fun>5 fun>6 morefun>1 Quote Link to comment Share on other sites More sharing options...
proggR Posted July 3, 2008 Author Share Posted July 3, 2008 Ya I was actually just reading that. I'm not sure if this will work for me for what I need. The first index represents a page number and the second index represents the post on that page. I'll need to access them independantly from eachother. What I want to happen is when I'm on page 3 it will go to the third element in the first index (the index that holds the page number) and I will be able to loop through each post index within that. So: //page 3 for($i=1;$i<=1;$i++){ include($post[3][$i].".php"); } is essentially what I want to do. $post holds the filename of each post and I'll be including them 5 at a time onto a page. I'm not sure how I could use the foreach for that. I was kind of hoping to be able to access them more like this if possible. Quote Link to comment Share on other sites More sharing options...
proggR Posted July 3, 2008 Author Share Posted July 3, 2008 Thank you teynon. That was really helpful to learn. Quote Link to comment Share on other sites More sharing options...
teynon Posted July 3, 2008 Share Posted July 3, 2008 You could jump straight to that level of the array: foreach ($post[3] as $value) { include ($value".php"); } Quote Link to comment Share on other sites More sharing options...
proggR Posted July 3, 2008 Author Share Posted July 3, 2008 Oh, duh. Thanks! Is there any reason why arrays in php don't seem to play nice with a normal for loop? In C/Java I know I'd be able to access them this way. Apples and oranges obviously, just wondering if theres a reason or if maybe I was just having bad luck. Thank you a lot though. I'll make the changes tonight and see how it works out. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 3, 2008 Share Posted July 3, 2008 The following foreach($categories as $key => $category) { echo 'Category #' . ($key+1) . '<br />'; foreach($category as $page) { echo $page . '<br />'; } echo '<hr />'; } does the same as for ($i = 0;$i<=$max;$i++){ for ($j = 0;$j<=5;$j++){ echo $array[$i][$j]; } } $categoriesis formatted like so: // category 1 $categories[0][0] = 'page1'; $categories[0][1] = 'page2'; $categories[0][2] = 'page3'; // category2 $categories[1][0] = 'page1'; $categories[1][1] = 'page2'; $categories[1][2] = 'page3'; Quote Link to comment Share on other sites More sharing options...
teynon Posted July 3, 2008 Share Posted July 3, 2008 PHP works fine with arrays. Your logic you posted is wrong... Doing a loop like for ($i=1; $i<=1; $i++) that loop will execute one time... Because you are saying while variable i is less than or equal to 1, do this. After the first loop, i = 2.. Quote Link to comment Share on other sites More sharing options...
proggR Posted July 3, 2008 Author Share Posted July 3, 2008 PHP works fine with arrays. Your logic you posted is wrong... Doing a loop like for ($i=1; $i<=1; $i++) that loop will execute one time... Because you are saying while variable i is less than or equal to 1, do this. After the first loop, i = 2.. Sorry I posted that wrong. It should have been ($i=1;$i<=$max;$i++). $max would just hold the number of pages that I need to use. But I've never had it work. Not even if I've replaced it with a numerical value instead. 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.