PHP array_keys() function with example

array_keys() work is utilized to get the keys of a cluster, it acknowledges an exhibit as contention and returns another exhibit containing keys.

Syntax:

array_keys(input_array, [value], [strict]);

Examples:

 Input:
    $per = array("name" => "Amit", "age" => 21, "gender" => "Male");
   
    Output:
    Array       
    (
        [0] => name    
        [1] => age     
        [2] => gender  
    )

PHP code 1: array with and without containing keys

<?php
    $per = array("name" => "Amit", "age" => 21, "gender" => "Male");
    print ("keys array...\n");
    print_r (array_keys($per));
    
    //array with out keys 
    $arr = array("Hello", "world", 100, 200, -10);
    print ("keys array...\n");
    print_r (array_keys($arr));    
?>

Output:

keys array...      
Array       
(
    [0] => name    
    [1] => age     
    [2] => gender  
)
keys array...      
Array       
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
) 

PHP code 2: Using value and strict mode

<?php
    $arr = array("101" => 100, "102" => "100", "103" => 200);
    
    print("output (default function call)...\n");
    print_r (array_keys($arr));    

    print("output (with checking value)...\n");
    print_r (array_keys($arr, 100));        

    print("output (with checking value & strict mode)...\n");
    print_r (array_keys($arr, 100, true));            
    
?>

Output

output (default function call)...
Array         
(             
    [0] => 101
    [1] => 102
    [2] => 103
)             
output (with checking value)...  
Array         
(             
    [0] => 101
    [1] => 102
)             
output (with checking value & strict mode)...
Array         
(             
    [0] => 101
)  

Leave a Comment

error: Alert: Content is protected!!