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
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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.