AppDividend
Latest Code Tutorials

PHP Array Keys Example | array_keys() Function Tutorial

0

PHP Array Keys Example | array_keys() Function Tutorial is today’s topic. The array_keys() function returns an array containing the keys. If a searchValue is specified, then only the keys for that values are returned. Otherwise, all the keys from an array are returned as the output. It determines if strict comparison (===) should be used during the search.

PHP Array Keys Example

PHP array_keys() is a built-in function in PHP and is used to return either all the keys of an array or the subset of the keys. The syntax for the array_keys is following.

array_keys(array,value,strict)

An array parameter is required, and it specifies the array.

The value parameter is optional, and you can specify a value, then only the keys with this value are returned.

The strict parameter is optional and used with the value parameter.

See the following example.

<?php

// app.php

$data = ['a' => 'krunal', 
        'b' => 'ankit',
        'd' => 'khushbu', 
        'f' => 'nehal', 
        'c' => 'rushabh', 
        'k' => 'krunal', 
        'n' => 'nehal'];
$output = array_keys($data);
print_r($output);

The output is following.

PHP Array Keys Example

 

So, we get the array of keys.

One thing to note about that if you have keys that are a long integer, such as ‘19214662291595‘, they will be considered as such on the 64bits system, but will be of type string on the 32 bits system.

See the following example.

<?php

// app.php

$appKeys = array('19214662291595' => null, 'AppDividend291595' => null);

foreach(array_keys($appKeys) as $key){
    echo gettype($key)."\n";
}

The output is following.

array_keys in PHP

 

Here’s how to get the first key and last key. See the below code.

Let’s get the first key of an array. For that, we can use the PHP array_shift function.

<?php

// app.php

$data = ['a' => 'krunal', 
        'b' => 'ankit',
        'd' => 'khushbu', 
        'f' => 'nehal', 
        'c' => 'rushabh', 
        'k' => 'krunal', 
        'n' => 'nehal'];
$keys = array_keys($data);
$firstKey = array_shift($keys);
echo $firstKey."\n";

The output is following.

first key of an array

 

We can also get the last key. For that, we can use the PHP array_pop() function.

See the below code.

<?php

// app.php

$data = ['a' => 'krunal', 
        'b' => 'ankit',
        'd' => 'khushbu', 
        'f' => 'nehal', 
        'c' => 'rushabh', 
        'k' => 'krunal', 
        'n' => 'nehal'];
$keys = array_keys($data);
$firstKey = array_pop($keys);
echo $firstKey."\n";

We will get the in the output because the last key is n.

Using the strict parameter

Now, let’s use the strict comparison and see the difference in the outputs.

<?php

// app.php

$app = array(10,20,30,21);
print_r(array_keys($app,"21",true));

In the above code, we are comparing the string 21 to the integer 21. The value is the same, but the datatype is different. Also, we are comparing the values under the strict flag. So the output will not be matched, and we see an empty array.

Using the strict parameter true

 

If we pass the false parameter, then it will give us the key of an array. See the below code.

<?php

// app.php

$app = array(10,20,30,21);
print_r(array_keys($app,"21",false));

The output is following.

Using the strict parameter false

 

One thing to note about that array_keys does not maintain the data-type of the keys when mapping them to the new array.  This will create an issue with the in_array and doing the lookup on characters from the string. There’s a lot of multidimensional array_keys function out there, but each of them only merges all the keys in one flat array.

Finally, PHP Array Keys Example | array_keys() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.