Support

Account

Home Forums Add-ons Repeater Field Problem with an array

Helping

Problem with an array

  • Hi,

    I’m using ACFs Repeater field to make an array. The array should look like this:

    array (size=3)
    ‘2020-06-06’ =>
    array (size=2)
    ‘title’ => string ‘Pyöräily’ (length=10)
    ‘description’ => string ’45min spinning, täysillä’ (length=26)
    ‘2020-06-17’ => =>
    array (size=2)
    ‘title’ => string ‘Jalkapallo’ (length=10)
    ‘description’ => string ‘Koko halli varattu’ (length=18)
    ‘2020-06-22’ => =>
    array (size=2)
    ‘title’ => string ‘Sulkapallo’ (length=10)
    ‘description’ => string ‘Koko halli varattu’ (length=18)

    But now it looks like this:

    array (size=3)
    0 =>
    array (size=2)
    ‘title’ => string ‘Pyöräily’ (length=10)
    ‘description’ => string ’45min spinning, täysillä’ (length=26)
    1 =>
    array (size=2)
    ‘title’ => string ‘Jalkapallo’ (length=10)
    ‘description’ => string ‘Koko halli varattu’ (length=18)
    2 =>
    array (size=2)
    ‘title’ => string ‘Sulkapallo’ (length=10)
    ‘description’ => string ‘Koko halli varattu’ (length=18)

    How I should fix my code so that those numbers would be replaced with dates?

  • Hmm, I guess there are two solutions to this. The first would be to create a nested repeater, i. e. one repeater for the event dates, and inside that, another repeater for event title and description. And then you loop over the dates, and in each iteration, you loop over the nested repeater to retrieve the other two field values. The documentation describes such a scenario in the section titled “Nested loops”.

    The other option would be to create an associative array, but for that, you can’t use array_push(), you need to use the square bracket notation with the date string as key, like so:
    $events[$event_day] = $single_event;

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Problem with an array’ is closed to new replies.