1

I have this array_map:

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

That returns the following:

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

How can I implode that statement so I can get just the strings?

I've tried the following:

    $test = implode(', ', $offices);
    var_dump($test);

The var_dump($test) returns the following: Array, Array, Array, Array, Array, Array, Array, Array, Array, Array.

I get the Array to string conversion error - What am I doing wrong? I'd love to just print out 1382 and then 1330

Sema
  • 95
  • 9
  • you're returning an `array`, just `return get_post_meta($post->ID, '_octopus_id', true);` result will be an array of strings. – Will B. Nov 12 '19 at 04:50
  • @fyrye, I need to keep the id key for another reason later in the code. – Sema Nov 12 '19 at 04:57
  • please update your question to reflect your needs, right now it is phrased as a misunderstanding of how `array_map()` works. And why you need the `id` associative index. – Will B. Nov 12 '19 at 05:01

2 Answers2

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

    print_r($offices);

    //or

    $officesStr = implode(', ', $offices);
    echo $officesStr;

$useLaterArr = [];
foreach ($offices as $office) {
    $useLaterArr = ['id' => $office];
}

echo "For later use :)\n";
print_r($useLaterArr);

$another = ['id' => $offices];
echo "Another way:\n";
print_r($offices);
Jimmix
  • 5,644
  • 6
  • 44
  • 71
  • Thanks so much @Jimmix, but I need to keep the id key for another reason later in the code. – Sema Nov 12 '19 at 04:57
  • @Sema What id do you mean? – Jimmix Nov 12 '19 at 04:58
  • I need to store the _octopus_id's as an id array to match it later in the code, so I can't return just, I need to return it as an array of 'id's. – Sema Nov 12 '19 at 05:00
  • @Sema after `implode()` it will be a comma separated string so what do you mean by `I need to return it as an array of 'id's.`??? – Alive to die - Anant Nov 12 '19 at 05:02
  • @AnantSingh---AlivetoDie, I guess the right thing that I was looking for was array_column. – Sema Nov 12 '19 at 05:03
  • @Jimmix, what’s the difference between using all that code and using array_column which worked - It's a legitimate question, I'm still learning. – Sema Nov 12 '19 at 05:05
1

You can use array_column() to fetch the array of all id

$offices = [
    ['id' => 1382],
    ['id' => 1330]
];

echo implode(', ', array_column($offices, 'id'));
catcon
  • 1,295
  • 1
  • 9
  • 18
  • this is exactly what I was looking for which returns the strings in an array - Is there a way to grab just the strings without the array after using array_column? – Sema Nov 12 '19 at 04:53
  • @Sema see my answer – Jimmix Nov 12 '19 at 04:55
  • I have the following piece of code next `$results = $octopus->get_all('employees/' . implode(array_column($offices, 'id')));` and I'd like to have for example `employees/1382`. I'm getting an error with the following code. – Sema Nov 12 '19 at 04:56