Author |
Message |
jonlumb
Spends far too much time on here
Joined: Thu Apr 23, 2009 6:44 pm Posts: 4141 Location: Exeter
|
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?
_________________ "The woman is a riddle inside a mystery wrapped in an enigma I've had sex with."
|
Sun May 10, 2009 4:40 pm |
|
 |
EddArmitage
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 9:40 pm Posts: 5288 Location: ln -s /London ~
|
Not sure exactly what you want to do, but there's an example here of an iteration over a 2D array. Edd
|
Sun May 10, 2009 5:18 pm |
|
 |
jonlumb
Spends far too much time on here
Joined: Thu Apr 23, 2009 6:44 pm Posts: 4141 Location: Exeter
|
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.
_________________ "The woman is a riddle inside a mystery wrapped in an enigma I've had sex with."
|
Sun May 10, 2009 5:27 pm |
|
 |
EddArmitage
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 9:40 pm Posts: 5288 Location: ln -s /London ~
|
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
|
Sun May 10, 2009 5:33 pm |
|
 |
jonlumb
Spends far too much time on here
Joined: Thu Apr 23, 2009 6:44 pm Posts: 4141 Location: Exeter
|
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.
_________________ "The woman is a riddle inside a mystery wrapped in an enigma I've had sex with."
|
Sun May 10, 2009 5:48 pm |
|
 |
EddArmitage
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 9:40 pm Posts: 5288 Location: ln -s /London ~
|
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:
|
Sun May 10, 2009 5:59 pm |
|
 |
jonlumb
Spends far too much time on here
Joined: Thu Apr 23, 2009 6:44 pm Posts: 4141 Location: Exeter
|
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.
_________________ "The woman is a riddle inside a mystery wrapped in an enigma I've had sex with."
|
Sun May 10, 2009 6:28 pm |
|
 |
big_D
What's a life?
Joined: Thu Apr 23, 2009 8:25 pm Posts: 10691 Location: Bramsche
|
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:
_________________ "Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari
Executive Producer No Agenda Show 246
|
Mon May 11, 2009 5:56 am |
|
 |
jonlumb
Spends far too much time on here
Joined: Thu Apr 23, 2009 6:44 pm Posts: 4141 Location: Exeter
|
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?
_________________ "The woman is a riddle inside a mystery wrapped in an enigma I've had sex with."
|
Fri May 15, 2009 12:10 pm |
|
 |
jonlumb
Spends far too much time on here
Joined: Thu Apr 23, 2009 6:44 pm Posts: 4141 Location: Exeter
|
Edit: Problem solved.
_________________ "The woman is a riddle inside a mystery wrapped in an enigma I've had sex with."
|
Fri May 15, 2009 1:55 pm |
|
 |
big_D
What's a life?
Joined: Thu Apr 23, 2009 8:25 pm Posts: 10691 Location: Bramsche
|
Glad I could be of help. 
_________________ "Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari
Executive Producer No Agenda Show 246
|
Sat May 16, 2009 7:07 am |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|
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.
_________________
|
Sat May 16, 2009 9:13 pm |
|
|