Array to string conversion on array_map

I'm using array_map to sort out an array with all of my _octopud_id's.

var_dump($offices): Returns the following:

array (size=500)
  0 = 
    array (size=1)
      'id' = string '1382' (length=4)
  1 = 
    array (size=1)
      'id' = string '1330' (length=4)

I need to input that array integer into the 'employees/' section but I can't figure out how - If I hardcode employees/6 I get the following result:

object(stdClass)[14592]
  public 'id' = int 6

What could I be doing wrong? I keep getting the Notice: Array to string conversion error on the $results = line.

/* Return an array of _octopus_ids */
$offices = array_map(
    function($post) {
        return array(
            'id' = get_post_meta($post-ID, '_octopus_id', true),
        );
    },
    $query-posts
);

var_dump($offices);

$results = $octopus-get_all('employee/' . implode($offices));
var_dump($results);

Topic wp-query php posts Wordpress

Category Web


Look closely at $offices:

array (size=500)
  0 => 
    array (size=1)
      'id' => string '1382' (length=4)
  1 => 
    array (size=1)
      'id' => string '1330' (length=4)

That is not an array of IDs. It's an array of arrays. $offices[0] is not '1382'. It's an array. So implode() on $offices is trying to concatenate arrays, which is what's causing your error.

The cause of the error is how you've used array_map. If you want to return this meta value for each post in an array, then you should only return the value, not a new array:

$offices = array_map(
    function($post) {
        return get_post_meta($post->ID, '_octopus_id', true);
    },
    $query->posts
);

Now $offices will be an array of IDs, which can be imploded.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.