PHP Array Unique Key
In PHP if we want to get all the keys then we can use array_keys() function. array_keys() function returns an array of all keys, it returns all the keys irrespective of its type (i.e. string or numeric)
General Description of array_key
| General Format |
array array_keys
( array $array
[, mixed $value
[, Boolean
$strict= false
]] )
|
||
| Parameters | $array: Input array | $value: This input is optional, if specified then all the keys containing the value will be returned. | $strict: It determines whether to check identically (= = =) or not |
| Return Value | Returns an array with all the keys mentioned, present in the array. | ||
PHP Unique Array Key Example 1:
<?php
$array=array("new delhi", "kolkata","mumbai","chennai"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array); echo "<br/><b>After extracting the keys uniquely:</b><br/>";print_r(array_keys(
$array));?>
Output:
Initially the values of $array1 is:
array(4) {
[0]=>
string(9) "new delhi"
[1]=>
string(7) "kolkata"
[2]=>
string(6) "mumbai"
[3]=>
string(7) "chennai"
}
After extracting the keys uniquely:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
Example 2:
<?php
$array=array(0=>"new delhi",1=>"kolkata",2=>"mumbai",3=>"chennai"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array); echo "<br/><b>After extracting the keys uniquely:</b><br/>";print_r(array_keys(
$array));?>
Output:
Initially the values of $array1 is:
array(4) {
[0]=>
string(9) "new delhi"
[1]=>
string(7) "kolkata"
[2]=>
string(6) "mumbai"
[3]=>
string(7) "chennai"
}
After extracting the keys uniquely:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
Example 3:
<?php
$array=array("n"=>"new delhi","k"=>"kolkata","m"=>"mumbai","c"=>"chennai"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array); echo "<br/><b>After extracting the keys uniquely:</b><br/>";print_r(array_keys(
$array));?>
Output:
Initially the values of $array1 is:
array(4) {
["n"]=>
string(9) "new delhi"
["k"]=>
string(7) "kolkata"
["m"]=>
string(6) "mumbai"
["c"]=>
string(7) "chennai"
}
After extracting the keys uniquely:
Array
(
[0] => n
[1] => k
[2] => m
[3] => c
)
Example 4:
<?php
$array=array("a"=>"new delhi","a"=>"kolkata","b"=>"mumbai","b"=>"chennai"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array); echo "<br/><b>After extracting the keys uniquely:</b><br/>";print_r(array_keys(
$array));?>
Output:
Initially the values of $array1 is:
array(2) {
["a"]=>
string(7) "kolkata"
["b"]=>
string(7) "chennai"
}
After extracting the keys uniquely:
Array
(
[0] => a
[1] => b
)
Example 5:
<?php
$array=array(0=>"new delhi","a"=>"kolkata","b"=>"mumbai",0=>"chennai"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array); echo "<br/><b>After extracting the keys uniquely:</b><br/>";print_r(array_keys(
$array));?>
Output:
Initially the values of $array1 is:
array(3) {
[0]=>
string(7) "chennai"
["a"]=>
string(7) "kolkata"
["b"]=>
string(6) "mumbai"
}
After extracting the keys uniquely:
Array
(
[0] => 0
[1] => a
[2] => b
)
Example 6:
<?php
$array=array(0=>"new delhi", "a"=>array("k"=>"kolkata","m"=>"mumbai"), 1=>"chennai"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array); echo "<br/><b>After extracting the keys uniquely:</b><br/>";print_r(array_keys(
$array));?>
Output:
Initially the values of $array1 is:
array(3) {
[0]=>
string(9) "new delhi"
["a"]=>
array(2) {
["k"]=>
string(7) "kolkata"
["m"]=>
string(6) "mumbai"
}
[1]=>
string(7) "chennai"
}
After extracting the keys uniquely:
Array
(
[0] => 0
[1] => a
[2] => 1
)
Example 7:
<?php
$array=array(0=>"new delhi","k"=>"kolkata","m"=>"mumbai",1=>"mumbai"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array); $search="mumbai"; echo "<br/><b>The keys of \"$search\":</b><br/>";print_r(array_keys(
$array,$search));?>
Output:
Initially the values of $array1 is:
array(4) {
[0]=>
string(9) "new delhi"
["k"]=>
string(7) "kolkata"
["m"]=>
string(6) "mumbai"
[1]=>
string(6) "mumbai"
}
The keys of "mumbai":
Array
(
[0] => m
[1] => 1
)
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.