Introduction
In order for you to understand the RFC , you first need to understand the problem and the reason why it was introduced.
Your array
$arr = array( array( 'firstname' => 'Bob', <-- 'lastname' => 'Tomato' | <-- ), | | array( | | 'firstname' => 'Larry', <-- | 'lastname' => 'Cucumber' <-| ) );
Getting a column
To get Bob & Larry or Tomato and Cucumber , you use more than one line of sample code:
$colums = array(); foreach ( array_map(null, $arr[0], $arr[1]) as $value ) { $colums[] = $value; } print_r($colums);
Output
Array ( [0] => Array ( [0] => Bob [1] => Larry ) [1] => Array ( [0] => Tomato [1] => Cucumber ) )
Dynamic version
The above code will only work if you know that the number of elements will be different, and will
$colums = array(); array_unshift($arr, null); foreach (call_user_func_array("array_map", $arr) as $value ) { $colums[] = $value; }
Live test
Or is it better to use Sill MultipleIterator
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL); foreach ( $arr as $v ) { $mi->attachIterator(new ArrayIterator($v)); } $colums = array(); foreach ( $mi as $v ) { $colums[] = $v; } print_r($colums);
Live test
Key name
If you need to get a key name, this is another method
$colums = array_reduce($arr, function ($a, $b) { foreach ( $b as $k => $v ) { $a[$k][] = $v; } return $a; });
Live test
Back to array_column
array_column simply assumes a process and getting all columns with a name will be as simple as the following:
print_r(array_column($arr,"lastname")); ^ | +--- This get value with key "lastname"
Live test
More sophisticated Senerio
Imagine you want your array to have this output
Array ( [Bob] => Tomato [Larry] => Cucumber )
Use old methods that you can use
$colums = array(); array_unshift($arr, null); foreach (call_user_func_array("array_map", $arr) as $value ) { $key = array_shift($value); $colums[$key] = current($value); } print_r($colums);
Live test
Now you can see that I had to use array_shift and current to get the first 2 elements. as your array grows this can get complicated, but array_column will simplify this
print_r(array_column($arr,"lastname","firstname")); ^ ^ | | Value Key (I wonder why position is backwards)
Output
Array ( [Bob] => Tomato [Larry] => Cucumber )
Finally, back to your question
What will be returned if a named key doesn't exist?
An empty array ... From your example
print_r(array_column($arr,"middlename")); ^ | it would try to check if any of your array has key middle man
He returns
Array <------- Otherwise returns empty array ( )
Conclusion
I used so that various examples are used with loop , array_map , array_reduce and MultipleIterator to explain what array_column trying to achieve.
As you can see, array_column much simplified, but I would advise you to play with the examples in RFC a bit, and this will let you understand it better if you still do not understand this, PHP is a flexible language that you can always implement your own version