Home >Backend Development >PHP Problem >PHP array sort key name remains unchanged

PHP array sort key name remains unchanged

PHPz
PHPzOriginal
2023-05-05 21:46:071002browse

In PHP programming, we often need to sort arrays. PHP provides many array sorting functions, such as sort(), asort(), ksort(), etc., which sort array elements in different ways. However, these functions change the order of the array keys by default. What if we want to preserve the order of the array keys? This article will introduce you to some methods.

  1. Use uksort() function

uksort() function is a very powerful function in PHP. It allows us to define the sorting function ourselves and sort it according to our method. Arrays are sorted. The usage of the uksort() function is as follows:

bool uksort(array &$array, callable $cmp_function)

Among them, $array is the array to be sorted, and $cmp_function is the custom sorting function.

In the custom sort function, we can use the two parameters $a and $b to compare two array elements and return the sorted result. If $a precedes $b, returns an integer less than 0; if $a precedes $b, returns an integer greater than 0; if $a and $b are equal, returns 0.

The following is a sample code:

<?php
function my_sort($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}

$array = array(
    'c' => 1,
    'a' => 2,
    'b' => 3,
);
uksort($array, 'my_sort');
print_r($array);
?>

The above code will output:

Array
(
    [c] => 1
    [a] => 2
    [b] => 3
)

As you can see, the key names of the array elements have not changed. Among them, the my_sort() function compares based on the size relationship of key names.

  1. Use the ksort() function and save the key name

The ksort() function is a commonly used function in PHP for sorting by key name. Unlike sort() and asort(), ksort() only changes the order of array elements without modifying the key names.

However, we can also save the original key name through some techniques. The specific method is: first store the key name and corresponding value in a new array, then sort the new array, and finally reassign the sorted values ​​to the original array. The sample code is as follows:

<?php
$array = array(
    'c' => 1,
    'a' => 2,
    'b' => 3,
);

$tmp_array = array();
foreach ($array as $key => $value) {
    $tmp_array[$key] = $value;
}

ksort($tmp_array);

$new_array = array();
foreach ($tmp_array as $key => $value) {
    $new_array[$key] = $value;
}

$array = $new_array;
print_r($array);
?>

The above code will output:

Array
(
    [c] => 1
    [a] => 2
    [b] => 3
)

As you can see, the key names of the array elements have not changed. However, this method requires the use of an additional array to save the original key names, which may cause memory problems if the number of array elements is large.

  1. Use the asort() function and save the key name

Similar to ksort(), the asort() function can also sort by value, but it will also change the array elements. Order.

Similarly, we can also save the original key names through some techniques. The specific method is: first store the key name and corresponding value in a new array, then sort the new array, and finally reassign the sorted values ​​to the original array. The sample code is as follows:

<?php
$array = array(
    'c' => 1,
    'a' => 2,
    'b' => 3,
);

$tmp_array = array();
foreach ($array as $key => $value) {
    $tmp_array[$key] = $value;
}

asort($tmp_array);

$new_array = array();
foreach ($tmp_array as $key => $value) {
    $new_array[$key] = $value;
}

$array = $new_array;
print_r($array);
?>

The above code will output:

Array
(
    [c] => 1
    [a] => 2
    [b] => 3
)

As you can see, the key names of the array elements have not changed. Similarly, this method also requires the use of additional arrays to save the original key names, which may cause memory problems.

To sum up, the above three methods can preserve the order of the array key names, and you can choose according to the specific situation. However, it should be noted that if there are duplicate key names in the array, the key names may be missing after sorting, and this needs to be handled according to the specific situation.

The above is the detailed content of PHP array sort key name remains unchanged. 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