How php reassembles a two-dimensional array into another array

04-12-2024

In PHP, a two-dimensional array means that an array contains one or more arrays. Two-dimensional arrays are created in the form of nested arrays. Usually, programmers need to deal with a large amount of data in complex applications. In this case, two-dimensional arrays can be used as a powerful tool to store, organize and process data.

In this article, we will discuss how to reassemble a two-dimensional array into a new array.

Initial ArrayLet's first define a two-dimensional array containing some student information, as shown below:

$students = array( array('name' => 'John', 'age' => 21, 'gender' => 'Male'), array('name' => 'Jane', 'age' => 19, 'gender' => 'Female'), array('name' => 'Jim', 'age' => 24, 'gender' => 'Male'), array('name' => 'Jenny', 'age' => 20, 'gender' => 'Female'));

This two-dimensional array contains four internal arrays, and each internal array contains three key-value pairs.

Reassemble the array Now we want to reassemble this two-dimensional array into a one-bit array of key-value pairs. The value will be the name of each student, and the key will be the gender of each student.

To accomplish this task, we need to use the foreach loop to traverse the initial array and add the name element of each internal array to the new array.

$new_array = array(); foreach ($students as $student) { $new_array[$student['gender']][] = $student['name']; }

Within this loop, we first retrieve the gender and name of each student by accessing the $student array defined in the first foreach statement. Then we use another foreach statement to create a list of elements in the new array. This list of elements consists of a key-value pair, where the key is the gender of the student and the value is an array containing the names of all students of the same sex. Each element of this array consists of a name, which is added to the array corresponding to the key by the [] operator.

We can use the following command to output the new array:

print_r($new_array);

The output is as follows:

Array( [Male] => Array ( [0] => John [1] => Jim ) [Female] => Array ( [0] => Jane [1] => Jenny ) )

This new array now takes each student's gender as the key and creates a name array for each gender. The result is a simplified, more readable and operable data structure.

Summary In this article, we introduced how to reassemble a two-dimensional array into a one-dimensional array of key-value pairs. We use the foreach loop to traverse the initial array and create a new array with the students' names. This new array takes students' gender as the key, and creates a name array for each gender. This process can significantly simplify the data structure and make it easier to read, manage and process.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us