PHP Array Unique Key


 

PHP Array Unique Key

PHP Array Unique Key: Sometimes we need to extract the keys (uniquely) from an array. PHP provides array_keys() function to find out the same. This tutorial has several examples on associative array, numeric array, and multi-dimensional array and their respective output.

PHP Array Unique Key: Sometimes we need to extract the keys (uniquely) from an array. PHP provides array_keys() function to find out the same. This tutorial has several examples on associative array, numeric array, and multi-dimensional array and their respective output.

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));

?>

0

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

1

$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);

2

echo "<br/><b>After extracting the keys uniquely:</b><br/>";

print_r(array_keys($array));

?>

3

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

4

$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);

5

echo "<br/><b>After extracting the keys uniquely:</b><br/>";

print_r(array_keys($array));

?>

6

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

7

$array=array(0=>"new delhi",

"a"=>array("k"=>"kolkata","m"=>"mumbai"),

1=>"chennai");

8

echo "<br/><b>Initially the values of \$array1 is:</b><br/>";

var_dump($array);

echo "<br/><b>After extracting the keys uniquely:</b><br/>";

9

print_r(array_keys($array));

?>

Output:

0
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");

1

echo "<br/><b>Initially the values of \$array1 is:</b><br/>";

var_dump($array);

$search="mumbai";

2

echo "<br/><b>The keys of \"$search\":</b><br/>";

print_r(array_keys($array,$search));

?>

3

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
)

Ads