admin管理员组

文章数量:1431434

I'm using Roots Sage 9, which uses Laravel within WordPress, and am combining this with Advanced Custom Fields.

For this, I have created a Controller, where I use an array_map to iterate through the returned array, to extract all of the date.

Depending on the page, I am sometimes returning an array with a 2nd level array within it, whilst on other occasions I am only returning an array with a single level.

The problem I run into, is in instances I only have a single level array, where the 2nd array_map returns with an error (array_map(): Argument #2 should be an array), as no array is present.

I assumed a ternary would solve this, but it doesn't seem to be the case

My question is, is it possible to create a function that uses array_map for these purposes?

My data, when it works, looks like this:

Array ( 
 [0] => Array (
    [identifier] => xyz
    [description] => xyz
    [bib_sources] => Array ( [0] => 
        Array ( 
            [type] => Book [year] => 1956 
        )
    )
 )
)

My data, when it doesn't work, looks like this:

Array ( 
 [0] => Array (
    [identifier] => xyz
    [description] => xyz
    [bib_sources] => 
 )
)

My code looks like this:

public function states()
{
    //type
    $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') ?? []);
    }
}

I'm using Roots Sage 9, which uses Laravel within WordPress, and am combining this with Advanced Custom Fields.

For this, I have created a Controller, where I use an array_map to iterate through the returned array, to extract all of the date.

Depending on the page, I am sometimes returning an array with a 2nd level array within it, whilst on other occasions I am only returning an array with a single level.

The problem I run into, is in instances I only have a single level array, where the 2nd array_map returns with an error (array_map(): Argument #2 should be an array), as no array is present.

I assumed a ternary would solve this, but it doesn't seem to be the case

My question is, is it possible to create a function that uses array_map for these purposes?

My data, when it works, looks like this:

Array ( 
 [0] => Array (
    [identifier] => xyz
    [description] => xyz
    [bib_sources] => Array ( [0] => 
        Array ( 
            [type] => Book [year] => 1956 
        )
    )
 )
)

My data, when it doesn't work, looks like this:

Array ( 
 [0] => Array (
    [identifier] => xyz
    [description] => xyz
    [bib_sources] => 
 )
)

My code looks like this:

public function states()
{
    //type
    $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') ?? []);
    }
}
Share Improve this question edited May 6, 2019 at 9:07 Ashkas asked May 6, 2019 at 6:44 AshkasAshkas 1771 gold badge2 silver badges8 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

returns with an error (array_map(): Argument #2 should be an array), as no array is present

The $states['s_bib_source_c_bib_sources'] ?? [] is only setting $states['s_bib_source_c_bib_sources'] if it isn't already set. If it's set and it's not an array, then array_map would throw the "should be an array" error. So you might better off use type-casting:

isset( $states['s_bib_source_c_bib_sources'] ) ? // wrapped
  (array) $states['s_bib_source_c_bib_sources'] : []

EDIT: Correct me if the $bib_sources['c_bib_sources_abbreviated_reference'] is not a WP_Term instance..

The get_term() part here is also confusing, because term IDs can never be the same and whatever the taxonomy is, the taxonomy's terms are saved in the same database table:

get_term_by( 'id', $bib_sources['c_bib_sources_abbreviated_reference']->term_id, 'bib_sources') ?? null

and the whole code could have probably been rewritten to:

$bib_sources['c_bib_sources_abbreviated_reference'] ?? null

However, if you want to re-verify that the array item c_bib_sources_abbreviated_reference is a valid term, then you could do so:

array_map( function( $bib_sources ) {
    $abb_ref = isset( $bib_sources['c_bib_sources_abbreviated_reference'] ) ?
        get_term( $bib_sources['c_bib_sources_abbreviated_reference'], 'bib_sources' ) : null;

    return [
        'type'     => $bib_sources['c_bib_sources_type'] ?? null,
        'year'     => $bib_sources['c_bib_sources_year'] ?? null,
        'abb_ref'  => is_wp_error( $abb_ref ) ? null : $abb_ref, // <- this
        'citation' => $bib_sources['c_bib_sources_citation'] ?? null,
    ];
}, isset( $states['s_bib_source_c_bib_sources'] ) ? (array) $states['s_bib_source_c_bib_sources'] : [] );

本文标签: phpConditional multidimensional arrays and arraymap