key


php128 apg

FETCH a key from an ARRAY.





This function returns returns the key of the array element that's currently being pointed to by the internal pointer.

This function does not move the pointer in any way.

If the internal pointer points beyond the end of the elements list or the array is empty, this function returns NULL.

Prior to PHP 7.0.0 the $array was passed by reference if possible, and by value otherwise.

Currently array is always passed by value.



<?php

mix key 
arr $array )


where,

$array The input ARRAY

?>

 $array 


The given input array.

Can be passed by reference.



  1 EXERCISE   

<?php

$arr01 
= ["countries" => ["Brasil""Chile"], 
             
"continent" => "South America"];

echo 
'The given ARRAY:<br>';
print_r($arr01);
echo 
'<br><br><br>';

$ndx01 key($arr01);

echo 
'The current index of ARRAY:<br>';
var_dump($ndx01);

?>

 RESULT   

The given ARRAY:
Array ( [countries] => Array ( [0] => Brasil [1] => Chile ) [continent] => South America )

The current index of ARRAY:
string(9) "countries"


  2 EXERCISE   

<?php

$arr02 
= array();

echo 
'The given ARRAY:<br>';
print_r($arr02);
echo 
'<br><br><br>';

$ndx02 key($arr02);

echo 
'The current index of ARRAY:<br>';
var_dump($ndx02);

?>

 RESULT   

The given ARRAY:
Array ()

The current index of ARRAY:
NULL


  3 EXERCISE   

<?php

$arr03 
= array(-80123"5");

echo 
'The given ARRAY:<br>';
print_r($arr03);
echo 
'<br><br><br>';

$ndx03 key($arr03);

echo 
'The current index of ARRAY:<br>';
var_dump($ndx03);

?>

 RESULT   

The given ARRAY:
Array ( [0] => -8 [1] => 0 [2] => 1 [3] => 2 [4] => 3 [5] => 5 )

The current index of ARRAY:
int(0)


  4 EXERCISE   

<?php

// The given ARRAY
$arr04d = [ -8123"5" ];

print_r($arr04d);
echo 
'<br>The current index number is : ';

// GETTING THE INDEX POINTED
$ndx04dc key($arr04d);

print_r($ndx04dc);
echo 
'<br><br><br>';

// REMOVING THE FIRST ELEMENT
array_shift($arr04d);

// THE NEW ARRAY
print_r($arr04d);
echo 
'<br>The current index number is : ';

// GETTING THE INDEX POINTED
$ndx04de key($arr04d);

print_r($ndx04de);

?>

 RESULT   

Array ( [0] => -8 [1] => 1 [2] => 2 [3] => 3 [4] => 5 )
The current index number is : 0

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 )
The current index number is : 0