array_column


php128 apg

RETURN the values from a single column in the input ARRAY.





If an ARRAY of objects is provided in the $input, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get and __isset magic methods.

The value of $column_key may be an INTEGER key of the column you wish to retrieve, or it may be a string key name for an associative ARRAY or property name.

$column_key may be also NULL to return complete arrays or objects, (this is useful together with $index_key to reindex the ARRAY).

The value of $index_key may be the INTEGER key of the column, or it may be the STRING key name.

$index_key is cast as usual for ARRAY keys, (however, objects supporting conversion to STRING are also allowed).

Additionally the following key casts will occur:

STRINGs containing valid decimal INTEGERS, unless the number is preceded by a + sign, will be cast to the INTEGER type.
E.g. the key "8" will actually be stored under 8.
On the other hand "08" will not be cast, as it isn't a valid decimal INTEGER.

FLOATs are also cast to INTEGERs, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.

BOOLs are cast to INTEGERs, too, i.e. the key true will actually be stored under 1 and the key false under 0.

NULL will be cast to the empty string, i.e. the key null will actually be stored under "".

ARRAYs and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

If multiple elements in the ARRAY declaration use the same key, only the last one will be used as all others are overwritten.

This function lacks features in versions prior to PHP 5.5.0.



<?php

arr array_column 
arr $input mix $column_key [, 
                                                  
mix $index_key NULL ] )
 

where,

$input The input ARRAY

$column_key The column of values to return

$index_key The column to use as the index/keys 
                                           
for the returned ARRAY

?> 

$input


A multi-dimensional ARRAY or an ARRAY of objects from which to pull a column of values from.



$column_key


The column of values to return.



$index_key


The column to use as the index/keys for the returned ARRAY.



  1 EXERCISE   

<?php

$arr01 
= [['BRA' =>"Brasil"
                
'POR' => "Portugal"
                
'JAP' => "Japan"] ];

$col01a array_column($arr01'BRA');

print_r($col01a);

echo 
'<br>';

$col01b array_column($arr01'POR');

print_r($col01b);

echo 
'<br>';

$col01c array_column($arr01'JAP');

print_r($col01c);

echo 
'<br>';

?> 

 RESULT   

Array ( [0] => Brasil )
Array ( [0] => Portugal )
Array ( [0] => Japan )


  2 EXERCISE   

<?php

$arr02 
= [['BRA' =>"Brasil"'Capital' => "Brasilia"], 
[
'POR' => "Portugal"'Capital' => "Lisboa"], 
[
'JAP' => "Japan"'Capital' => "Tokyo"] ];

$col02a array_column($arr02'Capital');

print_r($col02a);

echo 
'<br>';

?> 

 RESULT   

Array ( [0] => Brasilia [1] => Lisboa [2] => Tokyo )

  3 EXERCISE   

<?php

$arr03 

[[
'Id' => "BRA"'Country' =>"Brasil"'Capital' => "Brasilia"], 
[
'Id' => "POR"'Country' => "Portugal"'Capital' => "Lisboa"], 
[
'Id' => "JAP"'Country' => "Japan"'Capital' => "Tokyo"] ];

$col03a array_column($arr03'Id''Country');

print_r($col03a);

echo 
'<br>';

$col03b array_column($arr03'Capital''Country');

print_r($col03b);

echo 
'<br>';

?> 

 RESULT   

Array ( [Brasil] => BRA [Portugal] => POR [Japan] => JAP )
Array ( [Brasil] => Brasilia [Portugal] => Lisboa [Japan] => Tokyo )