Home  >  Article  >  Backend Development  >  php multidimensional to two-dimensional array

php multidimensional to two-dimensional array

PHPz
PHPzOriginal
2023-05-07 16:37:08625browse

In PHP, developers often need to deal with multi-dimensional data structures obtained from various resources, and these data structures may contain very complex data types. Multidimensional arrays are a very useful tool when dealing with these data structures, but in some cases, we may need to convert multidimensional arrays into two-dimensional arrays.

In this article, we will explore how to use PHP to convert multi-dimensional arrays into two-dimensional arrays for more general data processing needs.

  1. Define a multidimensional array

To demonstrate how to convert a multidimensional array into a two-dimensional array, we first need to define a multidimensional array. We will create a "Fruit" array with three main categories, each containing three different entries.

$fruits = array(

"apples" => array(
    array("brand" => "Granny Smith", "color" => "green", "price" => 0.75),
    array("brand" => "Fuji", "color" => "red", "price" => 0.50),
    array("brand" => "Golden Delicious", "color" => "yellow", "price" => 0.60)
),
"oranges" => array(
    array("brand" => "Valencia", "color" => "orange", "price" => 0.80),
    array("brand" => "Navel", "color" => "orange", "price" => 0.85),
    array("brand" => "Blood", "color" => "red", "price" => 1.10)
),
"bananas" => array(
    array("brand" => "Cavendish", "color" => "yellow", "price" => 0.25),
    array("brand" => "Lady Finger", "color" => "green", "price" => 0.30),
    array("brand" => "Red Dacca", "color" => "red", "price" => 0.35)
)

);

In the above array, we create an array of fruits containing three categories, each of which contains Three different brand, color and price combinations.

  1. Convert a multidimensional array to a two-dimensional array

Now that we have defined a multidimensional array and understand how it is organized, we can start writing code to Convert it to a 2D array.

We can use PHP's array_map function, which applies a callback function to each element in a multidimensional array and returns a new array containing the results of the callback function.

The following is a sample code that contains a callback function that converts a multidimensional array to a two-dimensional array:

$flat_fruits = array();
array_map(function($group) use (&$flat_fruits) {

foreach ($group as $item) {
    $flat_fruits[] = array(
        "category" => array_keys($group)[0],
        "brand" => $item["brand"],
        "color" => $item["color"],
        "price" => $item["price"]
    );
}

}, $fruits);

In the above code, we first create a new array named $flat_fruits to store our converted the result of. We then use the array_map function to iterate through each entry of the $fruits array and pass it to an anonymous function that performs some operation on that entry.

In this anonymous function, we first use a foreach loop to iterate through all possible items in each category. For each item, we wrap it in a new array and store the item's brand, color, price, and category (here we just used the name of the category). Finally, we add this new array to the $flat_fruits array, which becomes our converted 2D array.

  1. Output results

To verify that our code works, we can simply output the $flat_fruits array and see the converted values. The following is a code that outputs an array of $flat_fruits:

foreach ($flat_fruits as $item) {

echo $item["category"] . " > " . $item["brand"] . " (" . $item["color"] . ", $" . $item["price"] . ")\n";

}

In the above code, we use a foreach loop to Iterate over each item in the $flat_fruits array. For each item, we format its content into a string and print it to the terminal.

The output should resemble the following:

apples > Granny Smith (green, $0.75)
apples > Fuji (red, $0.5)
apples > Golden Delicious (yellow, $0.6)
oranges > Valencia (orange, $0.8)
oranges > Navel (orange, $0.85)
oranges > Blood (red, $1.1)
bananas > Cavendish ( yellow, $0.25)
bananas > Lady Finger (green, $0.3)
bananas > Red Dacca (red, $0.35)

As you can see, we successfully converted the multi-dimensional array into a two-dimensional array dimensional array, and the results are formatted and output. It can be seen that PHP's array_map function is a very useful tool for processing multi-dimensional arrays. It allows developers to easily operate on multi-dimensional data structures and easily apply some transformation logic to transform the data into any desired format.

The above is the detailed content of php multidimensional to two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Query value in php arrayNext article:Query value in php array