2

Below is foreach and the 3 dimension arrays, no problem with the looping but i cannot sepcify which array to echo, they must me the whole arrays like echo $subvalue, any better solutions with looping 3 dimension array? i actually feel weird with this looping. Thanks in adv

foreach ($stories as $key => $story){
    //echo "<br />";
        foreach($story as $subkey => $subvalue){
            echo $subvalue."<br />";
                foreach($subvalue as $key => $subsubvalue){
                    echo $subsubvalue."<br />";
                }
        }
}

Array
(
    [270] => Array
        (
            [uid] => 36
            [user_email] => [email protected]
            [sid] => 270
            [story_name] => Story C
            [photo_url] => Array
                (
                    [0] => story_photos/2012/0322/361332381418153311.jpg
                    [1] => story_photos/2012/0322/361332393792911587.jpg
                )

            [photo_added_date] => Array
                (
                    [0] => 1332381418
                    [1] => 1332393792
                )

        )

    [269] => Array
        (
        [uid] => 36
        [user_email] => [email protected]
        [sid] => 269
        [story_name] => Story B
        [photo_url] => Array
            (
                [0] => story_photos/2012/0322/361332381406580761.jpg
            )

        [photo_added_date] => Array
            (
                [0] => 1332381406
            )

    )

[268] => Array
    (
        [uid] => 36
        [user_email] => [email protected]
        [sid] => 268
        [story_name] => Story A
        [photo_url] => Array
            (
                [0] => story_photos/2012/0322/361332381393552719.jpg
            )

        [photo_added_date] => Array
            (
                [0] => 1332381393
            )

    )

)

1
  • I don't understand what you're trying to achieve here. Commented Mar 31, 2012 at 17:35

4 Answers 4

4

Why not try this :

foreach ($stories as $key => $story){
    if(is_array($story)){
        foreach($story as $subkey => $subvalue){
            if(is_array($subvalue)){
                foreach($subvalue as $key => $subsubvalue){
                    echo $subsubvalue."<br />";
                }
            } else {
                echo $subvalue."<br />";
            }
        }
    } else {
        echo $story."<br />";
    }
}

Also, I am not sure because your question isn't really clear or specified.

Sign up to request clarification or add additional context in comments.

Comments

3

Or

function echoArray( $array )
{
    foreach ($array as $key => $cell)
    {
        if ( true == is_array($cell) )
        {
           echoArray($cell);
        }
        else
        {
            echo "$cell<br />";
        }
    }
}

It works for N dimensionnal array

An improved version to know the depth and use different css class for depth and to use set the tag in which the value should be added:

Eg: for depth 0 the class will by arrayclass_0, for depth 1 arrayclass_1, etc...

/**
$array : The array
$depth: The depth ( you should always set it to 0)
$cssclassprefix: The css class prefix you want to set
$tag: the default tag to use to render
$arraytagkey: An optionnal key in your array to extract the tag to use
*/
function echoArray( $array, $depth=0, $cssclassprefix='arrayclass_', $tag='div', $arraytagkey = '' )
{
    if ( 0 != strcmp($arraytagkey) && isset($array[$arraytagkey]) )
    {
       $tag=$array[$arraytagkey];
    }
    foreach ($array as $key => $cell)
    {
        if ( true == is_array($cell) )
        {
           echoArray($cell, $depth+1, $cssclassprefix, $tag, $arraytagkey);
        }
        else
        {
            $matches = array();
            if ( 0 != preg_match("/^(img|iframe|input)$/i",$tag) )
            {
                if ( 0 != strcasecmp('input',$tag) )
                {
                   echo "<input class='$cssclassprefix$depth' value='$cell' />";
                }
                else
                {
                   echo "<$tag class='$cssclassprefix$depth' src='$cell' />";
                }
            }
            else if( 0 != preg_match("/^(br|hr)$/i",$tag) )
            {
                echo "$cell<$tag />";
            }
            else
            {
                echo "<$tag class='$cssclassprefix$depth'>$cell</$tag>";
            }
        }
    }
}

6 Comments

Thanks for your reply, is this for unlimited depth loops? I actually just need second level loops.
It is for unlimited multiarray depth (in fact there is a depth limit for recursion in php5 but i don't remember it, because the limit is used to avoid infinite recursion, so segfault)
@grifos it works, but how do i call each $value? for example $value['user_email'].
I don't really get what you want, because the function is generic and will display each array cell (so each value) and recursively loop when a cell is an array. I edited my post to add the array key names in the loop, it might help, you.
Yes, it will display each array cell but i mean when output to HTML. I would like something like <div><?php $value['user_email'] ?></div>, so i can call each value in html. Hope you get what i mean. thx again
|
0

This doesn't really answer your question, but note, your third loop is this:

foreach ($subvalue as $key => $subsubvalue){

But you've already used $key in the first loop:

foreach ($stories as $key => $story){

You should change the third to:

foreach ($subvalue as $subsubkey => $subsubvalue){

Comments

0

Are you just wanting to loop through and get access to the photo_urls and other data? If that's the case then here's a simple example of how you can access the data:

foreach($stories as $key => $story)
{

    // here you can echo the $store['user_email']  etc
    echo 'Email: ' . $story['user_email'] . '<br />';

    // loop over the photo urls
    foreach($story['photo_url'] as $photo_url)
    {
        echo 'Photo URL: ' . $photo_url . '<br />';
    }

    // loop over the photo added dates
    foreach($story['photo_added_date'] as $photo_added_date)
    {
        echo 'Photo Date: ' . $photo_added_date . '<br />';
    }

}

If you're wanting to recursively search the array then the other answers are what you want.

1 Comment

Yes, i just want to have second level loops

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.