array_keys


php128 apg

RETURNS all the keys or a subset of the keys of a given ARRAY.





This function returns all the keys in the given ARRAY.



<?php

arr array_keys 
( arr $array )

where,

$array = The given ARRAY

?>

<?php

arr array_keys 
( arr $array , 
                         
mix $search_value [, 
                         
bool $strict = FALSE ] )

where,

$array = The given ARRAY

$search_value = The key values to be searched

$strict 
= To control if strict comparison will used or not

?>

 $array 


An array containing keys to return.



 $search_value 


If specified, then only keys containing these values are returned.

If a $search_value is specified, then only the keys for that value are returned.

Otherwise, all the keys from the ARRAY are returned.



$strict


To control if the check is made over types or not.

STRICT MODE
VALUES PURPOSE
0 (FALSE) stric mode will not be used
1 (TRUE) stric mode will be used
ed48


  1 EXERCISE   

<?php

$var01 
= ['continents' => ['South America', 'Europe'], 
                        
'countries' => ['Brasil', 'Portugal']]; 

var_export($var01);

echo 
'<br><br><br>';

$ndx01 = array_keys($var01);

var_export($ndx01);

?>

 RESULT   

GIVEN ARRAY

array ( 'continents' => array ( 0 => 'South America', 1 => 'Europe', ), 'countries' => array ( 0 => 'Brasil', 1 => 'Portugal', ), )


ARRAY KEYS

array ( 0 => 'continents', 1 => 'countries', )



  2 EXERCISE   

<?php

$var02 
= [  0 => "0", 
                
"1" => 0, 
                 
5 => "five", 
                 
6 => "six", 
                 
7 => "seven"  ];

echo 
'<br>The given ARRAY:<br>';

print_r($var02);

$var02akf = array_keys($var02);

echo 
'<br><br>The array keys ...
            <br>no search value<br>strict = FALSE:<br>'
;

print_r($var02akf);

$var02akt = array_keys($var02, true);

echo 
'<br><br>The array keys ...
               <br>no search value<br>strict = TRUE:<br>'
;

print_r($var02akt);


$var021amkt = array_keys($var02, "five", true);

echo 
'<br><br>The array keys ...
         <br>search value = "five"<br>strict = TRUE:<br>'
;

print_r($var021amkt);


echo 
'<br><br>The array keys ...
           <br>search value = "five"<br>strict = FALSE:<br>'
;

$var023amkf = array_keys($var02, "five", false);

print_r($var023amkf);

?> 

 RESULT   

The given ARRAY:
Array ( [0] => 0 [1] => 0 [5] => five [6] => six [7] => seven )

The array keys ...
no search value
strict = FALSE:
Array ( [0] => 0 [1] => 1 [2] => 5 [3] => 6 [4] => 7 )

The array keys ...
no search value
strict = TRUE:
Array ( [0] => 5 [1] => 6 [2] => 7 )

The array keys ...
search value = "five"
strict = TRUE:
Array ( [0] => 5 )

The array keys ...
search value = "five"
strict = FALSE:
Array ( [0] => 5 )


  3 EXERCISE   

<?php

$var03 
= 'QWERTY keyboard';

echo 
'<br><br><br>';

var_dump($var03);

$var03akf = array_keys($var03);

echo 
'<br><br><br>';

var_dump($var03akf);

$var03akt = array_keys($var03, true);

echo 
'<br><br><br>';

var_dump($var03akt);

$var031amkt = array_keys($var03, "five", true);

echo 
'<br><br><br>';

var_dump($var031amkt);


$var032amkt = array_keys($var03, "FIVE", true);

echo 
'<br><br><br>';

var_dump($var032amkt);

$var033amkt = array_keys($var03, "FIVE", false);

echo 
'<br><br><br>';

var_dump($var033amkt);

echo 
'<br><br><br>';

var_dump($var03);

?> 

 RESULT   

In PHP 7.4.x we will have a series of Warnings: ...,
in PHP 8.0.x, we will have a single Fatal error: ....