0

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; 
6
  • echo $venue[0]; the $venue variable is a single item array Commented Apr 16, 2014 at 22:07
  • If possible, you should fix the way you get the data, you seem to be making too many loops. Commented Apr 16, 2014 at 22:10
  • @jeroen - The data is coming from a repeatable custom field within Wordpress (using Advanced Custom Fields plugin) - I'm unsure how exactly I can 'fix' this data Commented Apr 16, 2014 at 22:13
  • Why the first item has 2 places in separate array? You should put these in a single array and create another foreach loop. Commented Apr 16, 2014 at 22:13
  • 1
    I'm not familiar with your plugin, but personally I would try to solve the problem at the root. When using plugins and third-party CMS's, a quick fix like the answers below might be easier though. Commented Apr 16, 2014 at 22:15

2 Answers 2

5

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I think he can have many places in there, that is why this is an array, if there are at least 2 places it wont work properly. EDIT: He has 2 places and each in separate array... so wrong.
@Linek i dont think so - see the 1st item in his array.
@user574632 Yea, I edited my comment, this is not correct though, there is no consistency in this.
Changed the answer. Let the OP decide which way is correct, depending on his data.
@wickywills see my comment on your question to jeroen, you are not doing anything wrong
|
3

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

1 Comment

Actually Linek, I modified the code to what you had before you removed your code, and ended up with a cleaner array!! So thanks for that! I've had to accept Oleg's answer, but you get a +1 from me :). Now I just need to find a good resource to learn about dealing with arrays - currently my weakness!

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.