What will return array_column if the key name does not exist?

In accordance with https://wiki.php.net/rfc/array_column array_column is planned to be added to PHP in the near future. But I have problems understanding RFC. What will be returned if the named key does not exist?

Example:

$arr = array( array( 'firstname' => 'Bob', 'lastname' => 'Tomato' ), array( 'firstname' => 'Larry', 'lastname' => 'Cucumber' ) ); $middlenames = array_column($arr, 'middlename'); 
+6
source share
2 answers

According to: https://wiki.php.net/rfc/array_column

If the corresponding indexKey cannot be found, the value will be entered with an integer starting from zero.

An example used in the RFC:

 $mismatchedColumns = array( array( 'a' => 'foo', 'b' => 'bar', 'e' => 'baz' ), array( 'a' => 'qux', 'c' => 'quux', 'd' => 'corge' ), array( 'a' => 'grault', 'b' => 'garply', 'e' => 'waldo' ), ); $foo = array_column($mismatchedColumns, 'a', 'b'); 

The results in $foo are equal:

 Array ( [bar] => foo [0] => qux [garply] => grault ) 

Essentially, the value of a becomes the new value of the array, and the key is b . When the source array does not contain the key b , it creates an index of 0 and uses this instead. If there were several keys that did not exist, they would be incremental from 0.

Studying their examples a bit, he hints that when you cannot match the value in the original array, you will not get the element of the array at all. This means that if you were looking for a single value in an array and there was none, it returned an empty array.


PS I obviously never used this function, so much of it is an interpretation of the RFC.


On the other hand, this function was accepted for inclusion in PHP and was originally proposed by Ben Ramsey with a final vote of 38 in favor and 6 against. A discussion of the mailing list can be found here: http://grokbase.com/t/php/php-internals/126nxxa80p/draft-rfc-array-column-function . See Also https://github.com/php/php-src/pull/257

+5
source

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

+7
source

All Articles