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:
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

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:

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

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 :

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;
}

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/