array_key_first


php128 apg

GETS the first key of an ARRAY.





This function return NULL if $array is empty.

This function is only available from PHP 7.3.0.

However, as you may have noticed, there are other functions which lead to similar results.



<?php

mix array_key_first 
arr $array )
 

where,

$array The ARRAY to get the first key
             
?>
 

$array


The input ARRAY.



  1 EXERCISE   

<?php      

function getfirst($arr)    
{   if(
function_exists('array_key_first'))
    {    
    
$k01 array_key_first($arr); 
    echo 
'<br>Array:<br>';    
    
var_dump($arr);    
    echo 
'<br><br>The FIRST key:<br>';    
    
var_dump($k01);    
    echo 
'<br><br>';
    }
else
{
echo 
'The function: array_key_first - is not available,br><br>';
}    
}    
        
$arr01a = [ 281832, [ 3218] ];     

$arr01b = [ 28'18'32, [ 'THIRTY-TWO'"eighteen"] ];     

$arr01c = [ 2997924586.67428E-11, [ 'c'"G" ] ];     

$arr01d = ['01013''0B0011''0x20b',      
              [
'octal''binary''hexadecimal' ] ];     

$arr01e = ["Countries""continentes",      
             [
"Brasil""Portugal""Japan"],      
             [
"South America""Europe""Asia"] ];     
                 

$arr01f = ["countries" => ["Brasil""Chile"],     
                   
"continent" => "South America"];    
                 
getfirst($arr01a);  

getfirst($arr01b);    

getfirst($arr01c);    

getfirst($arr01d);    

getfirst($arr01e);    

getfirst($arr01f);    

?> 

 RESULT   

Array:
array(5) { [0]=> int(2) [1]=> int(8) [2]=> int(18) [3]=> int(32) [4]=> array(3) { [0]=> int(32) [1]=> int(18) [2]=> int(8) } }

The FIRST key:
int(0)

Array:
array(5) { [0]=> int(2) [1]=> int(8) [2]=> string(2) "18" [3]=> int(32) [4]=> array(3) { [0]=> string(10) "THIRTY-TWO" [1]=> string(8) "eighteen" [2]=> int(8) } }

The FIRST key:
int(0)

Array:
array(3) { [0]=> int(299792458) [1]=> float(6.67428E-11) [2]=> array(2) { [0]=> string(1) "c" [1]=> string(1) "G" } }

The FIRST key:
int(0)

Array:
array(4) { [0]=> string(5) "01023" [1]=> string(6) "0B0011" [2]=> string(5) "0x20b" [3]=> array(3) { [0]=> string(5) "octal" [1]=> string(6) "binary" [2]=> string(11) "hexadecimal" } }

The FIRST key:
int(0)

Array:
array(4) { [0]=> string(9) "Countries" [1]=> string(11) "continentes" [2]=> array(3) { [0]=> string(6) "Brasil" [1]=> string(8) "Portugal" [2]=> string(5) "Japan" } [3]=> array(3) { [0]=> string(13) "South America" [1]=> string(6) "Europe" [2]=> string(4) "Asia" } }

The FIRST key:
int(0)

Array:
array(2) { ["countries"]=> array(2) { [0]=> string(6) "Brasil" [1]=> string(5) "Chile" } ["continent"]=> string(13) "South America" }

The FIRST key:
string(9) "countries"