PHP array_keys

Summary: in this tutorial, you will learn how to use the PHP array_keys() function to get the keys of an array.

Introduction to the PHP array_keys function

The PHP array_keys() function accepts an array and returns all the keys or a subset of the keys of the array.

array_keys ( array $array , mixed $search_value , bool $strict = false ) : arrayCode language: PHP (php)

In this syntax:

  • $array is the input array.
  • $search_value specifies the value of the keys to search for.
  • $strict if it sets to true, the array_keys() function uses the identical operator (===) for matching the search_value with the array keys. Otherwise, the function uses the equal opeartor (==) for matching.

The array_keys() function returns an array that contains all the keys in the input array.

PHP array_keys() function examples

Let’s take some examples of using the array_keys() function.

1) Using the array_keys() function example

The following example shows how to get all keys of an indexed array:

<?php

$numbers = [10, 20, 30];

$keys = array_keys($numbers);

print_r($keys);Code language: HTML, XML (xml)

Output:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
)Code language: PHP (php)

How it works.

  • First, define an array that contains three numbers.
  • Second, use the array_keys() function to get all the keys of the $numbers array.
  • Third, display the keys.

Since the $numbers is an indexed array, the array_keys() function returns the numeric keys of the array.

The following example uses the array_keys() function to get the keys of the array whole value is 20:

<?php

$numbers = [10, 20, 30];

$keys = array_keys($numbers, 20);

print_r($keys);
Code language: HTML, XML (xml)

Output:

Array
(
    [0] => 1
)Code language: PHP (php)

The array_keys() function returns the key 1 because key 1 contains the value 20.

2) Using PHP array_keys() function with an associative array example

The following example illustrates how to use the array_keys() function with an associative array:

<?php

$user = [
	'username' => 'admin',
	'email' => '[email protected]',
	'is_active' => '1'
];

$properties = array_keys($user);

print_r($properties);
Code language: HTML, XML (xml)

Output:

Array
(
    [0] => username
    [1] => email
    [2] => is_active
)Code language: PHP (php)

How it works.

  • First, define an associative array $user that contains three keys username, email, and is_active.
  • Second, get the keys of $user array using the array_keys() function.
  • Third, show the returned keys.

The following example uses the array_keys() function to get the keys whose values equal 1:

$user = [
	'username' => 'admin',
	'email' => '[email protected]',
	'is_active' => '1'
];

$properties = array_keys($user, 1);

print_r($properties);Code language: PHP (php)

Output:

Array
(
    [0] => is_active
)Code language: PHP (php)

The array_keys() function returns one key, which is is_active. However, the is_active contains the string '1', not the number 1. This is because the array_keys() uses the equality (==) operator for comparison in searching by default.

To enable the strict equality comparison (===) when searching, you pass true as the third argument of the array_keys() function like this:

<?php

$user = [
	'username' => 'admin',
	'email' => '[email protected]',
	'is_active' => '1'
];

$properties = array_keys($user, 1, true);

print_r($properties);Code language: HTML, XML (xml)

Output:

Array
(
)Code language: JavaScript (javascript)

Now, the array_keys() function returns an empty array.

Finding array keys that pass a test

The following function returns the keys of an array, which pass a test specified a callback:

<?php

function array_keys_by(array $array, callable $callback): array
{
	$keys = [];
	foreach ($array as $key => $value) {
		if ($callback($key)) {
			$keys[] = $key;
		}
	}

	return $keys;
}Code language: HTML, XML (xml)

The following example uses the array_keys_by() function above to find the keys that contain the string '_post':

<?php

$permissions = [
	'edit_post' => 1,
	'delete_post' => 2,
	'publish_post' => 3,
	'approve' => 4,
];

$keys = array_keys_by($permissions, function ($permission) {
	return strpos($permission, 'post');
});

print_r($keys);Code language: HTML, XML (xml)

Summary

  • Use the PHP array_keys() function to get all the keys or a subset of keys in an array.
Did you find this tutorial useful?