PHP array_keys() Function (original) (raw)

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 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><mi>y</mi><mo separator="true">,</mo><mi>m</mi><mi>i</mi><mi>x</mi><mi>e</mi><mi>d</mi></mrow><annotation encoding="application/x-tex">array , mixed </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">mi</span><span class="mord mathnormal">x</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span></span></span></span>search_value , bool $strict = false ) : arrayCode language: PHP (php)

In this syntax:

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

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

`<?php

$numbers = [10, 20, 30]; keys=arraykeys(keys = array_keys(keys=arraykeys(numbers);

print_r($keys);`Code language: PHP (php)

Try it

Output:

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

How it works.

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=arraykeys(keys = array_keys(keys=arraykeys(numbers, 20);

print_r($keys);`Code language: PHP (php)

Try it

Output:

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

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

Using PHP array_keys() function with an associative array #

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=arraykeys(properties = array_keys(properties=arraykeys(user);

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

Try it

Output:

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

How it works.

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

`<?php

$user = [ 'username' => 'admin', 'email' => '[email protected]', 'is_active' => '1' ]; properties=arraykeys(properties = array_keys(properties=arraykeys(user, 1);

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

Try it

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=arraykeys(properties = array_keys(properties=arraykeys(user, 1, true);

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

Try it

Output:

Array ( )Code language: PHP (php)

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,callablearray, callable array,callablecallback): array { $keys = []; foreach ($array as key=>key => key=>value) { if ($callback($key)) { keys[]=keys[] = keys[]=key; } }

return $keys;

}`Code language: PHP (php)

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

`<?php

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

return $keys;

}

$permissions = [ 'edit_post' => 1, 'delete_post' => 2, 'publish_post' => 3, 'approve' => 4, ]; keys=arraykeysby(keys = array_keys_by(keys=arraykeysby(permissions, function ($permission) { return strpos($permission, 'post'); });

print_r($keys);`Code language: PHP (php)

Try it

Output:

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

Summary #

Did you find this tutorial useful?