0

Im trying to make a neat, structured list of, in this case, films and showdates.

$shows = array(
    array(
        "Thursday" => array(
            "17:00",
            "19:00")),
    array(
        "Friday" => array(
            "16:30",
            "18:45"
            "20:10")),
    array(
        "Saturday" => array(
            "18:30",
            "21:00"))
);

The problem is that I cant seem to be able to print the things out in a reasonable way. The days are supposed to be dynamic, not hard corded as of this case.

for ($row = 0; $row < $shows.length(); $row++) //Haven't got a clue about the 'length()'
{
    print $shows[$row] . "<br>"; //Print the day.

    for (

   $col = 0; $col < $shows[$row].length(); $col++) //Loop through each day.
    {
        print (">" . $shows[$row][$col] . "<br>"); //Print each time of the day.
    }

}

And what im trying to do is to print out each day with the corresponding times. Should come out as something like this.

Thursday - 17:00
           19:00

Friday   - 16:30
           18:45
           20:10
1
  • 1
    You seem to be used to javascript right? In PHP there is a control structure specifically design to iterate through elements in arrays - foreach. Most of the answers in here use it for that reason. Commented Dec 20, 2010 at 12:27

2 Answers 2

1
foreach ($shows as $show) {
    foreach ($show as $day => $times) {
        echo $day;
        foreach ($times as $time) {
            echo $time;
        }
    }
}

But, really, you should simplify this a little like this:

$shows = array(
    array('day' => 'Saturday', 'times' => array('17:00', '19:00')),
    …
);

foreach ($shows as $show) {
    echo $show['day'];
    foreach ($show['times'] as $time) {
        echo $time;
    }
}

Or, to do it really properly and in a computer parseable manner:

$shows = array(
    strtotime('2010-12-24 17:00:00'),
    strtotime('2010-12-24 19:00:00'),
    …
);

$lastDate = null;
foreach ($shows as $show) {
    if (!$lastDate || date('Y-m-d', $show) != date('Y-m-d', $lastDate)) {
        echo date('l', $show);
    }
    echo date('H:i', $show);
    $lastDate = $show;
}

:o)

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

2 Comments

Worked out really well. But if I am to get those dates from an mysql database (datetime-field), how would I build up such an array? I'm returning Array ([0] => Array ( [date] => 2011-01-05 17:30:00 )) at the moment.
@Xavio That's pretty close to the last case, isn't it? You can just work with the array you get from your SQL query, just modify the loop as necessary. I'm sure you can figure it out. :)
0

In PHP to get the number of elements in the array you use the count function.

for ($row = 0; $row < count($shows); $row++) {
        foreach($shows[$row] as $key => $vals) {
                echo "$key - ";
                $first = true;
                foreach($vals as $val) {
                        if($first) {
                                echo "\t$val\n";
                                $first = false;
                        } else {
                                echo "\t\t$val\n";
                        }
                }
        }
}

Ideone Link

Comments

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.