0
array (size=1551884)
   0 => 
      array (size=1)
         'entity_id' => string '131813' (length=6)
   1 => 
      array (size=1)
         'entity_id' => string '213808' (length=6)
   2 => 
      array (size=1)
         'entity_id' => string '712885' (length=6)

is it possible to convert it to single array without the key 'entity_id' without a loop?

array
   0 =>
     131813
   1 =>
     213808
   2 =>
     712885

I have tried this one :

call_user_func_array('array_merge', $array)

but somehow is only returning 1 element

UPDATE:

here are the benchmark results from the given answers to this question:

php version > 5.6

array_column: 0.20802903175354
foreach: 0.46231913566589
array_map: 1.021989107132

php version > 7

array_column: 0.079965829849243
foreach: 0.15323305130005
array_map: 0.28970503807068
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

2 Answers2

3

This is also possible with array_column.

$result = array_column($your_array, 'entity_id');
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
2

You can do this very easily with array_map like this:

$result = array_map(function($value) {
    return $value['entity_id'];
}, $originalArray);

Working example: https://3v4l.org/JOEMI

Of course you could also do it with a foreach loop:

$result = [];
foreach($originalArray AS $entity) {
    $result[] = $entity['entity_id'];
}

Working example: https://3v4l.org/9J5XH

I prefer the first option personally.

Update: the accepted answer is clearly the best way. Do that! Leaving this here for comparison.

jszobody
  • 28,495
  • 6
  • 61
  • 72