Support

Account

Home Forums General Issues Saving repeater/nested repeater values to an associative array Reply To: Saving repeater/nested repeater values to an associative 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