x404.co.uk http://x404.co.uk/forum/ |
|
PHP - foreach issue http://x404.co.uk/forum/viewtopic.php?f=4&t=434 |
Page 1 of 1 |
Author: | jonlumb [ Sun May 10, 2009 4:40 pm ] |
Post subject: | PHP - foreach issue |
Ok, never used PHP before in my life, but have to do so for my uni course. I have been trying to use the foreach command but with a 2D array rather than a 1D array, and it doesn't seem to like it. Is there any way around this? |
Author: | EddArmitage [ Sun May 10, 2009 5:18 pm ] |
Post subject: | Re: PHP - foreach issue |
Not sure exactly what you want to do, but there's an example here of an iteration over a 2D array. Edd |
Author: | jonlumb [ Sun May 10, 2009 5:27 pm ] |
Post subject: | Re: PHP - foreach issue |
I am trying to produce a discussion board (uni assignment). My plan for each page was to create an array along the following lines: [Post Title],[Username],[PostTime],[Message] [Post Title2],[Username2], [PostTime2],[Message2] etc… Then use the Foreach to call a function with each entry in the row as a parameter, which would then create the 'box' that each post would reside in. All the examples I can see on that page only go through what would call a 1D array (ie just a single column) rather than an array with multiple columns. |
Author: | EddArmitage [ Sun May 10, 2009 5:33 pm ] | ||||||||||||||||||
Post subject: | Re: PHP - foreach issue | ||||||||||||||||||
The example I was refferring to was the following:
Could you not do something along the lines of:
I'm by no means an expert, though Edd |
Author: | jonlumb [ Sun May 10, 2009 5:48 pm ] |
Post subject: | Re: PHP - foreach issue |
The problem seems to be that it wants to treat it as a kind of list, running the loop for every single entry in the array, rather than letting me do it on a row by row basis. |
Author: | EddArmitage [ Sun May 10, 2009 5:59 pm ] | |||||||||
Post subject: | Re: PHP - foreach issue | |||||||||
When you say you have an array: [Post Title],[Username],[PostTime],[Message] [Post Title2],[Username2], [PostTime2],[Message2] Do you mean you have something equivalent to:
|
Author: | jonlumb [ Sun May 10, 2009 6:28 pm ] |
Post subject: | Re: PHP - foreach issue |
No, in effect I have the following: $a = array(); $a[0][0] = "PostTitle"; $a[0][1] = "Username"; $a[0][2] = "PostTime"; $a[1][0] = "PostTitle2"; $a[1][1] = "Username2"; $a[1][2] = "PostTime2"; $a[2][0] = "PostTitle3"; $a[2][1] = "Username3"; $a[2][2] = "PostTime3"; etc EDIT: I think what I'm going to have to do is create a whole group of arrays as mentioned above (one for each entry) and then maybe a final array that will contain the variable name for each of the other arrays, and then use a foreach loop. |
Author: | big_D [ Mon May 11, 2009 5:56 am ] | ||||||||||||||||||||||||||||||||||||
Post subject: | Re: PHP - foreach issue | ||||||||||||||||||||||||||||||||||||
The foreach breaks only the first level of the array away. So :
If you want to automatically walk the second dimension as well:
Would output :
You don't need to use the "$index => $value" syntax in a foreach loop, but I find it useful, as you have access to the outermost array index each time. In this case, the "$outIndex" contains the 0 - 2 of the outermost array and $index the index of the inner array ('title', 'username', 'time'). This can be very useful at times - E.g. copying items from an array to properties of a class:
|
Author: | jonlumb [ Fri May 15, 2009 12:10 pm ] |
Post subject: | Re: PHP - foreach issue |
Dave, that's awesome, the first example is perfect for what I need. Got it working with a function in there and all is delicious. Now, where's the next part for me to fall flat on my face over? |
Author: | jonlumb [ Fri May 15, 2009 1:55 pm ] |
Post subject: | Re: PHP - foreach issue |
Edit: Problem solved. |
Author: | big_D [ Sat May 16, 2009 7:07 am ] |
Post subject: | Re: PHP - foreach issue |
Glad I could be of help. ![]() |
Author: | Nick [ Sat May 16, 2009 9:13 pm ] |
Post subject: | Re: PHP - foreach issue |
This is the first time I've ventured into the Open Source forum for aaaages. I completely missed this thread. I still think we need a general help forum for stuff like this. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |