ACF multi level repeaters and array_map

I’ve been using Sage 9 for a new project, which is my first exposure to using anything laravel related.

I’ve seen a number of members here recommending the use of array_maps, for extracting data in the controllers. Having played with it a bit, I can see the advantage of doing so.

Where I have run into a problem though, is when I have multilevel repeaters - that is repeaters that have another repeater inside of it.

I’ve managed to get it working when the 2nd level repeater is active, but there are instances where the 2nd level repeater isn’t used, so that the argument 2nd level array_map, returns with with this error error, “array_map(): Argument #2 should be an array”.

I assumed I could use a ternary, but it seems to be entirely ignored, so it keeps running into the error when the 2nd level repeater isn’t active. This is the function I’ve created.

  public function states()
	$type = get_field('c_p_c_p_s_e_type', $post_id, false, false);
	if($type == 'State & Edition') {

	    return array_map(function($states) {
	      return [
	         'identifier' => $states['s_identifier'] ?? null,
	         'description' => $states['s_description'] ?? null,
	         'impression_description' => $states['s_impression_description'] ?? null,
	         'component_of_image' => $states['s_textual_numerical_comp_of_image'] ?? null,
	         'inscriptions' => $states['s_inscriptions'] ?? null,
 	         'artists_comments' => $states['s_artists_comments'] ?? null,
	         'authors_comments' => $states['s_authors_comments'] ?? null,
	         'image' => wp_get_attachment_image( $states['s_image'], 'medium' ) ?? null,
	         'related_works' => $states['s_related_works'] ?? null,
	         'final_state' => $states['s_final_state'] ?? null,
	         'ashkas' => $states['s_bib_source_c_bib_sources'] ?? null,
	         'bib_sources' =>  array_map(function($bib_sources) {
	             return [
			         'type' => $bib_sources['c_bib_sources_type'] ?? null,
			         'year' => $bib_sources['c_bib_sources_year'] ?? null,
			         'abb_ref' => get_term_by( 'id', $bib_sources['c_bib_sources_abbreviated_reference']->term_id, 'bib_sources') ?? null,
			         'citation' => $bib_sources['c_bib_sources_citation'] ?? null,
	          }, $states['s_bib_source_c_bib_sources'] ?? []),
	   }, get_field('c_states') ?? []);

How do I handle this kind of situation and prevent errors?

Hey @ashkas,

What does $states['s_bib_source_c_bib_sources'] evaluate to when the second-level repeater is inactive and you’re getting that error?

The ?? operator isn’t the ternary operator; it’s the null coalesce operator. This means your code will return your fallback empty array when $states['s_bib_source_c_bib_sources'] is undefined or set to null, but not if it’s something “falsey” like an empty string, for example.

The full ternary syntax is: $states['s_bib_source_c_bib_sources'] ? $states['s_bib_source_c_bib_sources'] : [];

And the shorthand equivalent is: $states['s_bib_source_c_bib_sources'] ?: []

Unlike the null coalesce operator, the ternary operator will return your fallback empty array if $states['s_bib_source_c_bib_sources'] is a non-null falsey value (like an empty string).

1 Like

This topic was automatically closed after 42 days. New replies are no longer allowed.