Current Location: Home> Latest Articles> array_keys() + array_values() + array_combine Reconstructing array

array_keys() + array_values() + array_combine Reconstructing array

M66 2025-06-07

The array_keys() function is used to return all key names in the array. Its basic usage is as follows:

 array_keys(array $array, mixed $value = null, bool $strict = false) : array
  • array : The array to operate.

  • value : If this parameter is specified, return the key names of all values ​​as that value.

  • strict : If set to true , strict type comparison is performed.

Example: Get all key names in the array

 $array = ["apple" => "green", "banana" => "yellow", "cherry" => "red"];
$keys = array_keys($array);
print_r($keys);

Output result:

 Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)

By using array_keys() we can easily get all key names in the array.

2. array_values() function

The array_values() function is used to return all values ​​in the array without including the key name. The basic usage is as follows:

 array_values(array $array) : array

Example: Get all values ​​in an array

 $array = ["apple" => "green", "banana" => "yellow", "cherry" => "red"];
$values = array_values($array);
print_r($values);

Output result:

 Array
(
    [0] => green
    [1] => yellow
    [2] => red
)

With array_values() we can get all the values ​​in the array, and these values ​​are arranged in order.

3. array_combine() function

The array_combine() function is used to combine two numbers into an array, with the element of the first array as the key name and the element of the second array as the value. The basic usage is as follows:

 array_combine(array $keys, array $values) : array
  • keys : The key name of the array.

  • values : The value of the array.

Example: Reconstruct an array using array_combine()

Suppose we have two arrays, one storing the name of the fruit and one storing the color of the fruit, we want to merge them into an associative array with the name of the fruit as the key and the color as the value.

 $keys = ["apple", "banana", "cherry"];
$values = ["green", "yellow", "red"];
$result = array_combine($keys, $values);
print_r($result);

Output result:

 Array
(
    [apple] => green
    [banana] => yellow
    [cherry] => red
)

With array_combine() we combine two arrays into an array of key-value pairs.

4. Use these three functions to reconstruct the array

Sometimes, we need to reconstruct an array so that its keys and values ​​are rearranged according to certain rules. We can use array_keys() , array_values() and array_combine() to achieve this.

Suppose we have an array containing fruit name and color information, but their keys and values ​​are already messed up. We want to reconstruct this array so that the fruit name is used as the key and the color is used as the value.

 $array = ["apple", "green", "banana", "yellow", "cherry", "red"];

// Get key names and values
$keys = array_keys($array, null, true);
$values = array_values($array);

// Recombining arrays
$result = array_combine($keys, $values);
print_r($result);

Output result:

 Array
(
    [0] => green
    [1] => yellow
    [2] => red
)