Home Forums General Issues Can a Select field's value be used to create an array?


Can a Select field's value be used to create an array?

  • Say I want to make a page for actors and I have included a list of movies for them to choose from within a repeater field with a dropdown select field (for multiple movies).

    These actor pages will share the same list of movies. I’d like a little information included with each movie, but don’t want to manually enter it separately on each page.

    Can I set the value and label to something like this for the backend:

    'Jaws',1975,'Thriller' : Jaws

    And then within a foreach loop create an array create something like for the frontend:

    $movie = get_sub_field('movie');
    $moviearray = array($movie);

    And, by my logic, importing the movie values into the array function will allow me to use the movie title, release year and genre within my page?

    I’m not an expert at PHP, sorry. Hope you guys can understand what I’m asking here.

  • you could enter something like
    as a value and then do something like

    $movie_array = array();
    $movie = get_sub_field('movie');
    $list = explode(',', $movie);
    foreach ($list as $value) {
      $parts = explode(':', $value);
      $movie_array[$parts[0]] = $parts[1];
  • Hi John, thank you for your reply. This would definitely solve my problem. I have a quick question, when looking at this:

    title:Jaws,year:1975,genre:Thriller : Jaws

    ACF won’t be confused by the multiple colons in the value?

    Really appreciate your help!

  • To set value : label for all select type field the colon must have a space both before and after the value.

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

The topic ‘Can a Select field's value be used to create an array?’ is closed to new replies.