6

How exactly can I apply array_column() to always get the first column of an array instead of getting the column by name?

I am seeking something like this is:

array_column($array,[0])

instead of:

array_column($array,"key");
3
  • 2
    array_column($array, array_keys($array)[0]); Commented Dec 9, 2015 at 20:11
  • Does not work for me. It seams array_column is hungry for and index name. Commented Aug 14, 2018 at 8:30
  • array_keys($array)[0] is the first index name. Do you have different column names in each row? Commented Feb 28 at 0:49

4 Answers 4

5

Try

array_column($array, array_shift(array_keys($array)));

from Return first key of associative array in PHP

Hope can help! :)

Sign up to request clarification or add additional context in comments.

2 Comments

Does not work for me. It seams array_column is hungry for and index name.
Why did you accept the answer if it doesn't work?
2

You can't do this with the array_column function, unless you know for certain what the key is for first element of each array is going to be ahead of time.

You'll need to do this with a foreach() and use reset() to get the first elements.

Comments

2

While array_column() will allow you to target a column using an integer, it needs to be an existing integer key. Otherwise, you will need to determine the first subarray's first key to gain access to that column dynamically.

Code: (Demo)

$array = [
    ["foo" => "bar1", "hey" => "now"],
    ["foo" => "bar2", "hey" => "what"],
    [0 => "zero", 1 => "one"]
];

var_export(array_column($array, 'foo'));  // get the column by name
echo "\n---\n";
var_export(array_column($array, 0));  // don't need to be a string
echo "\n---\n";
var_export(array_column($array, key(current($array))));  // access the first subarray, access its key
echo "\n---\n";
var_export(array_column($array, array_shift(array_keys($array))));  // this generates a Notice, and damages the array

Output:

array (
  0 => 'bar1',
  1 => 'bar2',
)
---
array (
  0 => 'zero',
)
---
array (
  0 => 'bar1',
  1 => 'bar2',
)
---

Notice: Only variables should be passed by reference in /in/hH79U on line 14
array (
  0 => 'zero',
)

If you use a loop or functional iterator, you can call current() or reset() to access the first element of each row, but if those first elements have different keys, then this is not technically a column of data. See what I mean in the demo -- you inadvertently grab values from different keys.

var_export(array_map('current', $array));
// ['bar1', 'bar2', 'zero']

...but current() only gets you the first column. For any column by position starting from 0, re-index all rows before calling array_column(). Demo

var_export(
    array_column(
        array_map(array_values(...), $array),
        1
    )
);

Comments

0

Here's a one-liner:

array_column($array, array_keys(array_values($array)[0])[0])

First it takes the first record of the multidimensional array:

array_values($array)[0]

The from that record, it gets the keys, and you can index any of the keys:

array_keys(array_values($array)[0])[0] # first column
array_keys(array_values($array)[0])[1] # second column
array_keys(array_values($array)[0])[2] # third column
array_keys(array_values($array)[0])[3] # fourth column

That gives you the name of the key by index that you can plug in array_column()

1 Comment

To clarify, this answer will return the values from each row which share a key with the first element of the first row. 3v4l.org/jiQNe Depending on row composition, this may return fewer results than the number of rows and/or return results that are not found in the first position of each row. 3v4l.org/jiQNe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.