0
0
PHPprogramming~5 mins

Array key and value extraction in PHP

Choose your learning style9 modes available
Introduction

Sometimes you want to get just the keys or just the values from an array to use them separately.

You want to list all the names (keys) from a list of people and their ages.
You want to get all the prices (values) from a product list to calculate the total.
You want to check if a certain key exists in an array.
You want to loop through only the values without caring about keys.
You want to create a new array from just the keys or just the values.
Syntax
PHP
<?php
// Extract keys from an array
$keys = array_keys($array);

// Extract values from an array
$values = array_values($array);
?>

array_keys() returns a new array with all the keys from the original array.

array_values() returns a new array with all the values from the original array, re-indexed starting at 0.

Examples
Extract keys and values from an associative array.
PHP
<?php
// Example with associative array
$person = ['name' => 'Alice', 'age' => 30];
$keys = array_keys($person);
$values = array_values($person);
print_r($keys);
print_r($values);
?>
Shows that extracting keys or values from an empty array returns an empty array.
PHP
<?php
// Example with empty array
$empty = [];
$keys = array_keys($empty);
$values = array_values($empty);
print_r($keys);
print_r($values);
?>
Extract keys and values from a simple indexed array.
PHP
<?php
// Example with indexed array
$numbers = [10, 20, 30];
$keys = array_keys($numbers);
$values = array_values($numbers);
print_r($keys);
print_r($values);
?>
Extract keys and values when the array has only one element.
PHP
<?php
// Example with single element array
$single = ['only' => 'one'];
$keys = array_keys($single);
$values = array_values($single);
print_r($keys);
print_r($values);
?>
Sample Program

This program creates an array of fruits with their colors, then extracts and prints the keys (fruit names) and values (colors) separately.

PHP
<?php
// Create an associative array
$fruits = [
    'apple' => 'red',
    'banana' => 'yellow',
    'grape' => 'purple'
];

// Print original array
echo "Original array:\n";
print_r($fruits);

// Extract keys
$fruit_names = array_keys($fruits);
// Extract values
$fruit_colors = array_values($fruits);

// Print keys
echo "\nExtracted keys (fruit names):\n";
print_r($fruit_names);

// Print values
echo "\nExtracted values (fruit colors):\n";
print_r($fruit_colors);
?>
OutputSuccess
Important Notes

Time complexity: Both array_keys() and array_values() run in O(n) time, where n is the number of elements.

Space complexity: They create new arrays, so space used is O(n).

A common mistake is expecting array_values() to keep original keys; it always re-indexes starting at 0.

Use array_keys() when you need to work with keys only, and array_values() when you want just the values without keys.

Summary

You can get all keys from an array using array_keys().

You can get all values from an array using array_values().

These functions help separate keys and values for easier processing.