Summary of php One dimensional and Two dimensional Array Key Sorting Method Example

  • 2021-08-03 09:22:23
  • OfStack

This paper summarizes the key sorting method of php1 dimensional and 2 dimensional arrays with examples. Share it for your reference. The specific methods are as follows:

In php array sorting 1 straight is a cliche problem, let's focus on 1 in php 1-dimensional array and 2-dimensional array sorting procedures, I believe we have a reference value.

Function: Reorder arrays.

Explanation: Bubble Sort (1-D Array) (2-D Array Sort)

The size of the data elements to be sorted is compared in pairs, and when the two data elements are found to be in opposite order, they are exchanged until there is no data element in reverse order

Imagine the ordered array R [1. N] standing upright. Think of each data element as a heavy bubble. Scan the array from bottom to top, and make any light bubbles that violate the principle "float" upwards. This is repeated until at last any two gases are the lighter one on top and the heavier one on the bottom.

/**
     * Bubble sorting (1 Dimensional array )(2 Sort of a key in dimensional array )
     * Compare the sizes of data elements to be sorted in pairs , When two data elements are found to be in opposite order, they are exchanged , Until there are no data elements in reverse order
     * Imagine an array being sorted R[1..N] Vertical erection , Think of each data element as a bubble with weight , Scan array from bottom to top , Where scanning light bubbles that violate the principle, , Just make it go up " Float ". This is repeated .
     * Until the end, any two qi are light. , The heavier one will stop at the bottom .
 */
function bubble_sort($array,$key=null) {
        $count = count($array);
        if($count < 0) {
            return false;
        }
        for($i = 0; $i < $count; $i++) {
            for($j = $count - 1; $j > $i; $j--) {
                if($key && isset($array[$key])){//2 Existence of dimensional array key
                    if($array[$j][$key] < $array[$j - 1][$key]) {
                        $tmp = $array[$j];
                        $array[$j] = $array[$j - 1];
                        $array[$j - 1] = $tmp;
                    }
                }else{ //1 Dimensional array
                    if($array[$j] < $array[$j - 1]) {
                        $tmp = $array[$j];
                        $array[$j] = $array[$j - 1];
                        $array[$j - 1] = $tmp;
                    }
                }
            }
        }
        return $array;
}

How to use array_multisort sort

Using array_multisort () to sort 2-digit groups by specified key values

In this example, a 2-dimensional array named $data is defined and sorted as follows

$data[] = array('volume' => 67, 'edition' => 2); 
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
 
// Gets a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}
 
// Data is based on volume Arranged in descending order, according to edition Ascending arrangement
// Put $data As the last 1 Parameters, sorted by common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
print_r($data);

The print result after execution is as follows:

Array 
(
    [0] => Array
        (
            [volume] => 98
            [edition] => 2
        )
    [1] => Array
        (
            [volume] => 86
            [edition] => 1
        )
    [2] => Array
        (
            [volume] => 86
            [edition] => 6
         
    [3] => Array
        (
            [volume] => 85
            [edition] => 6
        )
    [4] => Array
        (
            [volume] => 67
            [edition] => 2
        )
    [5] => Array
        (
            [volume] => 67
            [edition] => 7
        )
)

We only need to use sort () for 1-dimensional array sorting, and the corresponding asort ($arr); Function, which is to sort key values and maintain the original key-value relationship.

Same principle, rsort (); arsort (); krsort (); Functions are similar to sort () except that they are sorted in descending order. rsort (); ksort (); Same.

I hope this article is helpful to everyone's PHP programming.


Related articles: