wickywills
wickywills

Reputation: 4204

Two dimensional PHP array not outputting correctly

I have the following array of dates and places - basically each date will need to allow for multiple places. I am trying to display the array below into something like the following format:

20140411
Basingstoke
Salisbury

20140405
Basingstoke

20140419
Salisbury

... and so on

The array:

Array
(
    [20140411] => Array
        (
            [0] => Array
                (
                    [0] => Basingstoke
                )

            [1] => Array
                (
                    [0] => Salisbury
                )

        )

    [20140405] => Array
        (
            [0] => Array
                (
                    [0] => Basingstoke
                )

        )

    [20140419] => Array
        (
            [0] => Array
                (
                    [0] => Salisbury
                )

        )

    [20140427] => Array
        (
            [0] => Array
                (
                    [0] => Basingstoke
                )

        )

)

I believe I'm close, but I have always had some sort of mental block when it comes to working with arrays/keys etc. I am trying to do a nested foreach loop, which displays the dates fine, but am just getting "Array" outputted for the locations:

foreach ($dates as $date => $dateKey) {

    // Format the date
    $theDate = DateTime::createFromFormat('Ymd', $date);
    $theFormattedDate = $theDate->format('d-m-Y');

    echo '<h4>'.$theFormattedDate.'</h4>';

    foreach ($dateKey as $key => $venue) {
        echo $venue;
    }

}

Can someone spot where I'm going wrong here?

EDIT:

Here is where the arrays are being created, if that helps?

$dates = array();

while ( have_rows('course_date') ) : the_row(); 
    $theVenue = get_sub_field('venue');

    // Use the date as key to ensure values are unique
    $dates[get_sub_field('date')][] = array(
        $theVenue->post_title
    );
endwhile; 

Upvotes: 0

Views: 76

Answers (2)

Linek
Linek

Reputation: 1363

Places are nested 1 level deeper, you need one more foreach.

Never mind, that other guy said this plugin is supposed to work like that :)

Upvotes: 3

Oleg Dubas
Oleg Dubas

Reputation: 2348

In your case venue is an array.
It's always an array with the only element you can address as [0].
Thus...

foreach ($dates as $date => $dateKey) {

    // Format the date
    $theDate = DateTime::createFromFormat('Ymd', $date);
    $theFormattedDate = $theDate->format('d-m-Y');

    echo '<h4>'.$theFormattedDate.'</h4>';

    foreach ($dateKey as $key => $venue) {
        echo $venue[0];
    }

}

Or, in case you can have multiple venues in that last-level array, you can re-write the inner foreach adding another one:

foreach ($dates as $date => $dateKey) {

    // Format the date
    $theDate = DateTime::createFromFormat('Ymd', $date);
    $theFormattedDate = $theDate->format('d-m-Y');

    echo '<h4>'.$theFormattedDate.'</h4>';

    foreach ($dateKey as $key => $venues) {
        foreach($venues as $v) {
           echo $v;
        }
    }
}

Upvotes: 5

Related Questions