'Tom', 'age' => 18 ), array('na"/> 'Tom', 'age' => 18 ), array('na">

Home  >  Article  >  Backend Development  >  Why does PHP fail to reorder a two-dimensional array?

Why does PHP fail to reorder a two-dimensional array?

PHPz
PHPzOriginal
2023-04-18 09:06:09578browse

Problem Description

In PHP, for a two-dimensional array, sometimes it is necessary to sort according to a certain key value. We generally use the array_multisort() function to sort, but in some cases the sorting fails. .
For example: There is a two-dimensional array $arr, which needs to be sorted in descending order according to the key value "name". The code is as follows:

$arr = array(
    array('name' => 'Tom', 'age' => 18),
    array('name' => 'Jerry', 'age' => 20),
    array('name' => 'Bob', 'age' => 25),
    array('name' => 'Alice', 'age' => 22),
);

// 按照'name'进行降序排序
array_multisort(array_column($arr, 'name'), SORT_DESC, $arr);

However, in some cases, using the above code will cause the sorting to fail. , what are the reasons and solutions? The following will be analyzed for everyone.

Troubleshooting

In such a problem, generally the best way is to print out the array to be sorted and observe whether the sorted key values ​​are not defined or the data format is wrong. or other questions. If the format of the printed array is correct, you can try to debug the code.

Next, we use the var_dump() function to print the $arr array to see:

// 打印一下原数组,查看键名和键值
var_dump($arr);

Output results:

array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(3) "Tom"
    ["age"]=>
    int(18)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(5) "Jerry"
    ["age"]=>
    int(20)
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(3) "Bob"
    ["age"]=>
    int(25)
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(5) "Alice"
    ["age"]=>
    int(22)
  }
}

We can see that the key name and key value The format is correct, but why does the sorting fail?

Problem Analysis

We look at the above code again, and we can find that when using the array_multisort() function, the array_column() function is used to extract the key values ​​​​to be sorted, which may exist here question.

array_column() function can extract the value corresponding to a key name in a one-dimensional array, but if the array is multi-dimensional, it will only treat the first dimension as a one-dimensional array instead of the entire multi-dimensional array. Convert an array to a one-dimensional array. This can lead to incorrectly referencing index data when using the array_multisort() function. Below we explain this issue.

The variable $arr is a two-dimensional array. We use the array_column() function to extract the value with the key name "name". The result is as follows:

// 提取出键名为'name'的值
$name = array_column($arr, 'name');
var_dump($name);

Output result:

array(4) {
  [0]=>
  string(3) "Tom"
  [1]=>
  string(5) "Jerry"
  [2]=>
  string(3) "Bob"
  [3]=>
  string(5) "Alice"
}

As shown above, we only get a one-dimensional array, which will cause the values ​​corresponding to other key names to be lost during sorting. Therefore, we can try to convert the multi-dimensional array into a one-dimensional array, and then use the array_multisort() function to sort.

Solution

There are two ways to convert a multi-dimensional array into a one-dimensional array, which is to use the array_walk_recursive() function or foreach() to loop through the multi-dimensional array. Here we will take the array_walk_recursive() function as an example to illustrate.

Let’s take a look at how to use the array_walk_recursive() function to convert a multi-dimensional array into a one-dimensional array:

// 定义一个函数将多维数组转换为一维数组
function array_multi2single(&$data){
    static $result_array = array();
    array_walk_recursive($data, function($value, $key) use(&$result_array){
        $result_array[$key] = $value;
    });
    $data = $result_array;
}

// 将多维数组转换为一维数组
array_multi2single($arr);

The converted $arr array is as follows:

array(8) {
  ["name"]=>
  string(5) "Alice"
  ["age"]=>
  int(22)
  [0]=>
  string(3) "Tom"
  [1]=>
  string(5) "Jerry"
  [2]=>
  string(3) "Bob"
  [3]=>
  string(5) "Alice"
  [4]=>
  int(18)
  [5]=>
  int(20)
}

After converting the multidimensional array to a one-dimensional array, we can reuse the array_multisort() function to sort the array:

// 将多维数组转换为一维数组
array_multi2single($arr);
// 按照'name'进行降序排序
array_multisort(array_column($arr, 'name'), SORT_DESC, $arr);

The sorted array looks like this:

array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "Jerry"
    ["age"]=>
    int(20)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(3) "Bob"
    ["age"]=>
    int(25)
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(5) "Alice"
    ["age"]=>
    int(22)
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(3) "Tom"
    ["age"]=>
    int(18)
  }
}

After completing the multidimensional array conversion After that, we reuse the array_multisort() function to sort the array, and the sorting can proceed normally.

The above is the detailed content of Why does PHP fail to reorder a 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