Support

Account

Home Forums General Issues Saving repeater/nested repeater values to an associative array

Solving

Saving repeater/nested repeater values to an associative array

  • I looked around the forum to see if I could find others who had solved this problem, but none of the answers really gave me the clue I needed.

    Basically I have a field group “URLs and Codes” that is a repeater which contains two fields: URL and Code (also a repeater). Multiple codes can be associated to URLs something like this:

    URL A: Code 1, Code 3, Code 9
    URL B: Code 2
    URL C: Code 3, Code 2

    I need to fetch these values to be used within a larger function. Any help would be greatly appreciated.

    So far, I’m able to grab the URLs, but I can’t figure out how to store the codes that correspond to the URLs.

    if( have_rows ('target_urls', 'options')){
            $target_urls= array();
            $target_urls[]= get_sub_field('single_url');
            while ( have_rows ('target_urls', 'options')){
                the_row();
                foreach ($target_urls as $target_url) {
                    $target_url = get_sub_field('single_url');
                    $target_urls[] = $target_url;
                }
                if( have_rows ('site_codes', 'options')){
                    while ( have_rows('site_codes', 'options')){
                        the_row();
                        $site_code= get_sub_field('single_code');
                    }
                }
    
            }
        }
  • Use the get_field_object function instead of the loop and you can return the [‘value’] as an array.

  • The first thing you need to do is to figure out how you want the array to be formatted. I can see 2 ways to do this.

    
    // array format 1
    $rows = array(
      'url' => 'value from url field',
      'codes' => array(
        // array of codes
        'code 1',
        'code 2'
      )
    }
    

    to get the above

    
    $target_urls = array();
    if (have_rows('target_urls', 'options')) {
      while (have_rows('target_urls', 'options')) {
        the_row();
        $url = get_sub_field('single_url');
        $codes = array();
        if (have_rows('site_codes')) {
          while (have_rows('site_codes')) {
            the_row();
            $codes[] = get_sub_field('single_code');
          } // end while codes
          $target_urls[] = array(
            'url' => $url,
            'codes' => $codes
          );
        } // end if codes
      } // end while target urls
    } // end if target_urls
    
    
    // array format 2
    $rows = array(
      // array key is the url
      'value of url field' => array(
        // array of codes
        'code 1',
        'code 2'
      )
    )
    

    to get the above

    
    $target_urls = array();
    if (have_rows('target_urls', 'options')) {
      while (have_rows('target_urls', 'options')) {
        the_row();
        $url = get_sub_field('single_url');
        $target_urls[$url] = array();
        if (have_rows('site_codes')) {
          while (have_rows('site_codes')) {
            the_row();
            $target_urls[$url][] = get_sub_field('single_code');
          } // end while codes
        } // end if codes
      } // end while target urls
    } // end if target_urls
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.