0

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);
0

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.

1
  • thanks so much! Since I want to be able to keep the id as an array with all the values, how would I retrieve just the string '1382' numeric value and use that?
    – DevSem
    Nov 12 '19 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.