In this article, we will learn about PHP array_keys this is also one of the important topics which we need to tackle and understand if you want to know more about array_keys
kindly scroll down and read it thoroughly.
In PHP, array_keys is a function which returns all the keys or keys that contain a subset of an array
.
What is array_keys () used for in PHP?
The array_keys is a PHP built-in function that returns either a subset of the keys or all the keys that contain an array.
In addition, this array_keys function has takes 3
parameters out of these three one are mandatory and the other two were optional.
Syntax:
array_keys(array, value, strict)
Parameters:
Name | Description | Required / Optional | Type |
---|---|---|---|
input_array | Specified array. | Required | Array |
search_key_value | Value to be checked. | Optional | Array |
strict | As of PHP 5, this parameter determines if strict comparison (===) should be used during the search. | Optional | Boolean |
Example:
<?php
$a=array("Oppo"=>"VX19","HUAWEI"=>"Y9","SAMSUNG"=>"J5");
print_r(array_keys($a));
?>
Output:
Array
(
[0] => Oppo
[1] => HUAWEI
[2] => SAMSUNG
)
How get key from value in array in PHP?
To get a key from a key value pairs in an array you just need to use the array_search()
function.
For example:
<?php
$arr = array ('Glenn Magada Azuelo' => 'y', 'Angel Jude Suarez' => 'x', );
$cue = array_search ('y', $arr);
echo $cue;
?>
Output:
Glenn Magada Azuelo
How get the key of an object in PHP?
In PHP, to get the key of an object simply use the array_keys()
which is a built-in function in PHP.
For example:
<?php
$programmerDetails = (object) [
'firstName' => 'Glenn',
'middleName' => 'Magada',
'lastName' => 'Azuelo',
'countryName' => 'Philippines'
];
$allKeysOfEmployee = array_keys((array)$programmerDetails);
echo "All Keys are as follows"."\n";
foreach($allKeysOfEmployee as &$tempKey)
echo $tempKey."\n";
?>
Output:
All Keys are as follows
firstName
middleName
lastName
countryName
How do I get a key from multiple arrays?
The following program below is a solution to get a key from multiple
or multidimensional
arrays shift.
Example of multiple arrays:
$array = array (
0 =>
array (
'id' => '9',
'gallery_id' => '2',
'picture' => '56475832.jpg'
),
1 =>
array (
'id' => '8',
'gallery_id' => '2',
'picture' => '20083622.jpg'
),
2 =>
array (
'id' => '7',
'gallery_id' => '2',
'picture' => '89001465.jpg'
),
3 =>
array (
'id' => '6',
'gallery_id' => '2',
'picture' => '47360232.jpg'
),
4 =>
array (
'id' => '5',
'gallery_id' => '2',
'picture' => '4876713.jpg'
),
5 =>
array (
'id' => '4',
'gallery_id' => '2',
'picture' => '5447392.jpg'
),
6 =>
array (
'id' => '3',
'gallery_id' => '2',
'picture' => '95117187.jpg'
)
);
Example program:
function array_search_inner ($array, $attr, $val, $strict = FALSE) {
// Error is input array is not an array var dump
if (!is_array($array)) return FALSE;
// Loop the array and data type
foreach ($array as $cue => $inner) {
// Error if inner item is not an array (you may want to remove this line)
if (!is_array($inner)) return FALSE;
// Skip entries where search cue is not present
if (!isset($inner[$attr])) continue;
if ($strict) {
// Strict typing
// otherwise all the keys
will return false
if ($inner[$attr] === $val) return $cue;
} else {
// Loose typing
if ($inner[$attr] == $val) return $cue;
}
}
// We didn't find it
return NULL;
}
// Example usage
$cue = array_search_inner($array, 'id', 9);
PHP array key exists
This array_key_exists() is a function that checks an array of a specified key, and it will return true if the key exists and false if the key doesn’t exist.
Syntax:
array_key_exists(key, array)
For example:
<?php
$name=array("Glenn"=>"26","Jude"=>"27");
if (array_key_exists("Adones",$name)){
echo "Exists!";
}
else{
echo "Does not exist!";
}
?>
Output:
Key does not exist!
Related Articles
- PHP in_array function With Examples
- PHP Array Search With Advanced Example
- PHP Arrays (With Advanced Program Examples)
- Types of Loops in PHP (With Flowcharts and Program Examples)
- Constants in PHP (Definition With Examples)
Summary
This article discusses about PHP array_keys, and also tackles how to how to get key from the value in the array, how to get the key of an object, how do I get a key from multiple arrays, and PHP array key exists.
I hope this lesson has helped you learn PHP a lot. Check out my previous and latest articles for more life-changing tutorials that could help you a lot.