How to Get All Values from an Associative Array in PHP?
Last Updated :
11 Jul, 2024
Given an Associative Array, the task is to get all values from the associative array in PHP. To get all the values from an associative array, you can use various approaches such as foreach loop, array_values() function, and array_map() function. In this article, we will explore each approach with detailed explanations and code examples.
Approach 1: Get All Values from an Associative Array using foreach Loop
The basic approach is to use a foreach loop to iterate over the associative array and access each value.
PHP
<?php
$student = array(
"Maths" => 95,
"Physics" => 90,
"Chemistry" => 96,
"English" => 93,
"Computer" => 98
);
foreach ($student as $marks) {
echo "$marks\n";
}
?>
Approach 2: Get All Values from an Associative Array using array_values() Function
The array_values() function returns all the values of an array in a new array and then use foreach loop to access all values of the new array.
PHP
<?php
$student = array(
"Maths" => 95,
"Physics" => 90,
"Chemistry" => 96,
"English" => 93,
"Computer" => 98
);
$values = array_values($student);
foreach ($values as $value) {
echo "$value\n";
}
?>
Approach 3: Get All Values from an Associative Array using array_map() Function
The array_map() function applies a callback function to each value of an array and returns an array with the modified values. After getting the new modified array containing values, we use foreach loop to access all values of the array.
PHP
<?php
$student = array(
"Maths" => 95,
"Physics" => 90,
"Chemistry" => 96,
"English" => 93,
"Computer" => 98
);
$values = array_map(function($value) {
return $value;
}, $student);
foreach ($values as $value) {
echo "$value\n";
}
?>
Approach 4: Using the array_reduce() Function
This approach utilizes the array_reduce() function to accumulate all the values from an associative array into a single new array. array_reduce() iteratively applies a user-defined callback function to each element of the array, carrying the result forward to each subsequent element in a manner that "reduces" the array to a single value, which can be an array itself if designed that way.
Example: In this example, we use array_reduce() to extract all the values of a specific key from an associative array. This is particularly useful when you need to perform additional processing on the values, such as filtering or transforming them before accumulating.
PHP
<?php
$products = [
['id' => 101, 'name' => 'Keyboard', 'price' => 25],
['id' => 102, 'name' => 'Mouse', 'price' => 15],
['id' => 103, 'name' => 'Monitor', 'price' => 100]
];
// Get all 'price' values from the products array
$prices = array_reduce($products, function($carry, $item) {
$carry[] = $item['price'];
return $carry;
}, []);
print_r($prices);
?>
OutputArray
(
[0] => 25
[1] => 15
[2] => 100
)
Approach 4: Using array_column()
The array_column() function is useful for extracting the values from a single column in an array of arrays or an array of objects. It can also be used to extract all values from an associative array based on the keys.
Example
PHP
<?php
// Example associative array
$assocArray = [
'name' => 'John',
'age' => 30,
'city' => 'New York'
];
// Extract all values using array_column()
$values = array_column($assocArray, null);
print_r($values);
?>
OutputArray
(
[0] => John
[1] => 30
[2] => New York
)
Similar Reads
How to get specific key value from array in PHP ? In this article, we will see how to get specific key values from the given array. PHP array is a collection of items that are stored under keys. There are two possible types of keys: strings and integers. For any type of key, there is a common syntax to get a specific value by key â square brackets.
3 min read
Sort an Associative Array by Key in PHP Given an Associative Array, the task is to sort the associative array by its keys in PHP. There are different methods to sort Associative Array by keys, these are described below: Table of ContentUsing ksort() FunctionUsing uksort() FunctionConverting to a Regular Array for SortingUsing array_multis
3 min read
How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i
4 min read
How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using
2 min read
How to create an array with key value pairs in PHP? In PHP, an array with key-value pairs is called an associative array. It maps specific keys to values, allowing you to access elements using custom keys rather than numerical indices. Keys are strings or integers, while values can be of any data type.Here we have some common approaches to create an
2 min read