PHP 2D array sorting keeps the key name unchanged

Keywords: PHP less

To sort the key names assigned to a two-dimensional array, the first thing you think about is the array? Multisort function. I wrote about the use of array? Multisort before An article
There is not much nonsense. Let's take an example:

<?php
$data = array(
1001 => array(
'age' => 22,
'name' => 'Jiu Mo Zhi'
),
1007 => array(
'age' => 21,
'name' => 'Murong Fu'
),
1004 => array(
'age' => 27,
'name' => 'Joe helps the Lord'
)
);
//Based on fields age Pair array $data Arrange in descending order 

$ages = array_column($data, 'age');

array_multisort($ages, SORT_ASC, $data);

print_r($data);

Careful friends will see that the key name is reset, starting from 0, obviously this may not be the result we want, so how to keep the key name unchanged?

Let's take another example:

$data = array(
    1001 => array(
        'age' => 22,
        'name' => 'Jiu Mo Zhi'
    ),
    1007 => array(
        'age' => 21,
        'name' => 'Murong Fu'
    ),
    1004 => array(
        'age' => 27,
        'name' => 'Joe helps the Lord'
    )
);
//Based on fields age Pair array $data Arrange in descending order
$data = arraySort($data, "age", "desc" );
print_r($data);

/**
 * @desc arraySort php Two dimensional array sorting: natural sorting of array according to specified key
 * @param array $arr Array to sort
 * @param string $keys Specify the key for sorting
 * @param string $type Sort type asc | desc
 * @return array
 */
function arraySort($arr, $keys, $type = 'asc')
{
    $keysvalue = $new_array = array();
    foreach ($arr as $k => $v) {
        $keysvalue[$k] = $v[$keys];
    }

    if ($type == 'asc') {
        natsort($keysvalue);
    }
    if ($type == 'desc') {
        natsort($keysvalue);
        $keysvalue = array_reverse($keysvalue, TRUE); // Flip the order of elements in the original array,If the second parameter is specified as true´╝îThe key name of the element remains unchanged
    }
    foreach ($keysvalue as $k => $v) {
        $new_array[$k] = $arr[$k];
    }
    return $new_array;
}

Here we can also simplify the arraySort function. The processing results are the same:

/**
 * @desc arraySort php Two dimensional array sorting: natural sorting of array according to specified key
 * @param array $arr Array to sort
 * @param string $keys Specify the key for sorting
 * @param string $type Sort type asc | desc
 * @return array
 */
function arraySort($arr, $keys, $type = 'asc')
{
    $keysvalue = $new_array = array();
    foreach ($arr as $k => $v) {
        $keysvalue[$k] = $v[$keys];
    }

    $type == 'asc' ? asort($keysvalue) : arsort($keysvalue);
    foreach ($keysvalue as $k => $v) {
        $new_array[$k] = $arr[$k];
    }
    return $new_array;
}

From the above results, we can see:

The key name remains unchanged, the principle of implementation is very simple, first take out the key name, then sort the key name, and then form a new array according to the corresponding key name assignment.
As you can see, here we mainly use several core sorting functions of php

asort() sorts the associated array in ascending order by key value.

The arsort() function sorts the associated arrays in descending order of key values.

natsort() implements "natural sorting", that is, the sorting method of numbers from 1 to 9, the sorting method of letters from a to z, and the short one takes precedence. The index of the array remains associated with the cell value,
Note: in the natural sorting algorithm, the number 2 is less than the number 10. In the computer sorting algorithm, 10 is less than 2, because the first number in "10" is less than 2.

Posted by wanda46 on Tue, 03 Dec 2019 10:28:03 -0800