In this tutorial, you will learn how to split all the keys of a PHP array into a new array.
PHP array_keys() Function
With the help of the PHP array_keys()
function, you can split all the keys of an array into a new array.
This function takes three parameters and returns a new array with keys.
array_keys(array, value, strict)
array
(Required) – The array whose keys you want to split.value
(Optional) – This is an optional parameter, this parameter takes any value of an array whose key you want to get.strict
(Optional) – It is also optional, and by default it isFALSE
. If you want to get the key of an exact type of value, then passTRUE
.
<?php
$user = ["name" => "John Doe", "age" => "24", "email" => "jhon1[email protected]"];
$keys = array_keys($user);
print_r($keys);
Array
(
[0] => name
[1] => age
[2] => email
)
Example with the value parameter of the array_keys
<?php
$user = ["name" => "John Doe", "age" => "24", "email" => "[email protected]"];
$keys = array_keys($user, "24");
print_r($keys);
Array
(
[0] => age
)
Example with the strict parameter of the array_keys
<?php
$num = [8, 5, 45, "5", 7, "2"];
$key = array_keys($num, 5);
$key_strict = array_keys($num, "5", true);
print_r($key);
print_r($key_strict);
Array
(
[0] => 1
[1] => 3
)
Array
(
[0] => 3
)