1

I have an array like this:

Array
(
    [1197624] => Array
        (
            [0] => Array
                (
                    [datetime] => 2010-11-06 21:32:56
                    [movieID] => 1197624
                    [personID] => 0
                    [filename] => 1197624.jpg
                    [more indexes]...
                )
            [1] => Array
                (
                    [datetime] => 2010-11-06 21:25:53
                    [movieID] => 1197624
                    [personID] => 0
                    [filename] => 1197624.jpg
                    [more indexes]...
                )
        )
    [0987657] => Array
        (
            [0] => Array
                (
                    [datetime] => 2010-11-06 21:38:07
                    [movieID] => 0
                    [personID] => 0987657
                    [filename] => 0987657.jpg
                    [more indexes]...
                )
            [1] => Array
                (
                    [datetime] => 2010-11-06 21:55:09
                    [movieID] => 0
                    [personID] => 0987657
                    [filename] => 0987657.jpg
                    [more indexes]...
                )
        )
    [5467023] => Array
        (
            [0] => Array
                (
                    [datetime] => 2010-11-06 21:59:33
                    [movieID] => 5467023
                    [personID] => 0
                    [filename] => 5467023.jpg
                    [more indexes]...
                )


        )
)

I want to echo it like this (notice how the keys are grouped):

<div>
  <img src="../1197624.jpg" />
  <p>1197624</p>
  <ul>
    <li>2010-11-06 21:32:56</li>
    <li>2010-11-06 21:25:53</li>
  </ul>
</div>

<div>
  <img src="../0987657.jpg" />
  <p>0987657</p>
  <ul>
    <li>010-11-06 21:38:07</li>
    <li>2010-11-06 21:55:09</li>
  </ul>
</div>

<div>
  <img src="../5467023.jpg" />
  <p>5467023</p>
  <ul>
    <li>02010-11-06 21:59:33</li>
  </ul>
</div>

How to do it?

0

2 Answers 2

3
foreach ($array as $k => $vals) {
    echo "<div>\n";
    echo "  <img src=\"../" . $vals[0]["filename"] . "\" />\n";
    echo "  <p>" . $k . "</p>\n";
    echo "  <ul>\n";
    foreach ($vals as $v) {
        echo "    <li>" . $v["datetime"] . "</li>\n";
    }
    echo "  </ul>\n";
    echo "</div>\n";
}

A few assumptions here:

  • Each element in the top-level array will have at least one element as its value (so that the filename for the img tag can be obtained from the first element).
  • For each element of the top-level array, the filenames in the entry's values are identical (if not, the first one may not be the right one to choose).
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for taking the time to include also the HTML in your answer ;)
0

The rough structure will look like this:

foreach ($array as $key => $element)
 {
       ... echo opening div....

       echo $key; // 1197624
       ...
       echo $element[0]["datetime"]; // 2010-11-06 21:32:56
       echo $element[1]["datetime"]; // 2010-11-06 21:25:53
       ...

       ... echo closing div....


 }

2 Comments

Yes, but I dont know how many $element[0] are. I mean sometimes is $element[0] only and sometimes $element[786]
@Jonathan then do another foreach on them

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.