Array_column() only picking up last part of array

Uriahs Victor :

For the life of my I can't figure out how array_column() is working differently for my array than it is for the example in the PHP doc.

Here's my array(retrieved from a return):

  $array = array (
  0 => 
  array (
    'name' => 'Category: Test cat 1',
    'value' => '2',
    'tax' => 'category',
    'selected' => '1',
  ),
  1 => 
  array (
    'name' => 'Category: Uncategorized',
    'value' => '1',
    'tax' => 'category',
    'selected' => '1',
  ),
);

When I output:

$a = array_column($array, 'value', 'tax');        
print_r($a);

The result is:

Array
(
    [category] => 1
)

What i was expecting is:

Array
    (
     [category] => 2
     [category] => 1
    )

I tried making the array like how it is in the PHP doc for array_column():

$array = array (
  array (
    'name' => 'Category: Test cat 1',
    'value' => '2',
    'tax' => 'category',
    'selected' => '1',
  ),
  array (
    'name' => 'Category: Uncategorized',
    'value' => '1',
    'tax' => 'category',
    'selected' => '1',
  ),
);

I still get the same result

Array
(
    [category] => 1
)

Any explanation as to why I'm not getting the expected array is would be appreciated

Omar Abbas :

If you pass third argument in array_column($array, 'value', 'tax'); like that, it will use third argument as index of the resulting array, in your case you have same value present in the tax key of your inner arrays, so that's why it gets replaced by the value from last array.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=301624&siteId=1