Reply to topic  [ 12 posts ] 
PHP - foreach issue 
Author Message
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 6:44 pm
Posts: 4141
Location: Exeter
Reply with quote
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
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
Not sure exactly what you want to do, but there's an example here of an iteration over a 2D array.

Edd

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Sun May 10, 2009 5:18 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 6:44 pm
Posts: 4141
Location: Exeter
Reply with quote
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
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
The example I was refferring to was the following:
Code:
/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}


Could you not do something along the lines of:
Code:
$posts = //wherever you get your posts from
foreach ($posts as $post) {
    //whatever echo-ing or whathaveyou to output a post, using data contained in variable $post
}


I'm by no means an expert, though

Edd

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Sun May 10, 2009 5:33 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 6:44 pm
Posts: 4141
Location: Exeter
Reply with quote
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
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
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:

Code:
$post1 = array($post1title, $post1uid, $post1time, $post1msg);
$post2 = array($post2title, $post2uid, $post2time, $post2msg);
$posts = array($post1, $post2);

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Sun May 10, 2009 5:59 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 6:44 pm
Posts: 4141
Location: Exeter
Reply with quote
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
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
The foreach breaks only the first level of the array away.

So :

Code:
$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";

foreach( $a as $index => $values )
{
    echo $values[0].$values[1].$values[2];
}


If you want to automatically walk the second dimension as well:

Code:
$a[0]['title'] = "PostTitle";
$a[0]['username'] = "Username";
$a[0]['time'] = "PostTime";
$a[1]['title'] = "PostTitle2";
$a[1]['username'] = "Username2";
$a[1]['time'] = "PostTime2";
$a[2]['title'] = "PostTitle3";
$a[2]['username'] = "Username3";
$a[2]['time'] = "PostTime3";

foreach ($a as $outIndex => $values )
{
    foreach( $values as $index => $value )
    {
        echo $index."-".$outIndex." = ".$value."<br />\n";
    }
}


Would output :
Code:
title-0 = PostTitle
username-0 = Username
time-0 = PostTime
title-1 = PostTile2
username-1 = Username2
time-1 = PostTime2
title-2 = PostTitle3
username-2 = Username3
time-2 = PostTime3


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:

Code:
$posts = new postsClass();
foreach( $a as $outIndex => $values )
{
    $post = new PostClass();
    foreach($values as $index => $value )
    {
        $post->{$index} = $value;
    }
    $posts->list[$outIndex] = $post;
}

_________________
"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
Profile ICQ
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 6:44 pm
Posts: 4141
Location: Exeter
Reply with quote
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
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 6:44 pm
Posts: 4141
Location: Exeter
Reply with quote
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
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
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
Profile ICQ
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
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.

_________________
Image


Sat May 16, 2009 9:13 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 12 posts ] 

Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.