Home > Article > Backend Development > How to convert multi-dimensional array into one-dimensional array in php
When writing PHP programs, we often need to deal with arrays. Arrays in PHP can be single-dimensional or multi-dimensional, but in some cases, we need to convert multi-dimensional arrays into single-dimensional arrays. This article will show you how to convert a multi-dimensional array into a single-dimensional array using PHP.
1. Understanding multi-dimensional arrays and one-dimensional arrays
In PHP, a multi-dimensional array refers to an array that contains other arrays. That is to say, each element in the multi-dimensional array is an array. For example, the following is a two-dimensional array:
$fruit = array( 'apple' => array('type' => 'fruit', 'taste' => 'sweet'), 'banana' => array('type' => 'fruit', 'taste' => 'sweet'), 'carrot' => array('type' => 'vegetable', 'taste' => 'bitter') );
If you want to convert this two-dimensional array to a one-dimensional array, you can use array_merge_recursive() or a foreach loop to achieve this.
2. Use array_merge_recursive() function
array_merge_recursive() function can be used to merge multi-dimensional arrays. This function merges multiple arrays into a single array, retaining all values for the same key. For example, if you have two arrays with the same key "apple" and one contains "taste" and "color" information and the other contains "type" and "attribute" information, array_merge_recursive() will retain the All values, i.e.:
$fruit = array( 'apple' => array('taste' => 'sweet', 'color' => 'red'), 'banana' => array('taste' => 'sweet', 'color' => 'yellow') ); $vegetable = array( 'apple' => array('type' => 'fruit', 'attribute' => 'delicious'), 'carrot' => array('type' => 'vegetable', 'attribute' => 'healthy') ); $merged = array_merge_recursive($fruit, $vegetable);
The result will be a multi-dimensional array $merged, as shown below:
$array = Array ( [apple] => Array ( [taste] => Array ( [0] => sweet [1] => Array ( [0] => fruit ) ) [color] => red [attribute] => delicious ) [banana] => Array ( [taste] => sweet [color] => yellow ) [carrot] => Array ( [type] => vegetable [attribute] => healthy ) )
Since the array merged using the array_merge_recursive() function is a multi-dimensional array, you still need Use a foreach loop to convert it to a one-dimensional array.
3. Use the foreach loop
Using the foreach loop to convert a multi-dimensional array into a one-dimensional array is more flexible than using the array_merge_recursive() function. The basic idea of using a foreach loop to convert a multidimensional array to a one-dimensional array is to copy each value in the multidimensional array to a new one-dimensional array. For example, for the $fruit array above, you can convert it to a one-dimensional array using the following code:
$flat = array(); foreach($fruit as $key => $value) { if(is_array($value)) { $flat = array_merge($flat, array_flatten($value)); } else { $flat[$key] = $value; } }
This code uses recursion and the array_merge() function to flatten a multi-dimensional array. The array_flatten() function is defined as follows:
function array_flatten($array) { $result = array(); foreach($array as $key => $value) { if (is_array($value)) { $result = array_merge($result, array_flatten($value)); } else { $result[$key] = $value; } } return $result; }
This function calls itself recursively to convert a multi-dimensional array into a one-dimensional array.
4. Summary
Converting a multi-dimensional array to a one-dimensional array is a basic task in PHP development. In this article, we covered two methods: using the array_merge_recursive() function and using a foreach loop. Regardless of which method you choose, converting a multidimensional array to a one-dimensional array is a useful skill.
The above is the detailed content of How to convert multi-dimensional array into one-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!