How to sort arrays and data in PHP?
P粉068174996
P粉068174996 2023-10-17 15:33:51
0
2
510

This question is intended as a reference regarding array sorting issues in PHP. It's easy to think that your particular case is unique and deserves a new question, but most are actually just minor variations on one of the solutions on this page.

If your question was closed as a duplicate of this question, please only ask to reopen your question if you can explain why it is significantly different from all of the following questions.

How to sort an array in PHP?

How to sort acomplexarray in PHP?

How to sort an array of objects in PHP?


  1. Basic one-dimensional array; included. Multidimensional arrays, incl. Array of objects; includes. Sort one array based on another array

  2. Use SPL sorting

  3. Stable sorting

See 1. for a practical answer using PHP's existing functions, for an academically detailed answer about sorting algorithms (that PHP functions implement and that youmightneed in very, very complex situations), See 2.

P粉068174996
P粉068174996

reply all (2)
P粉476475551

Okay,decezehas most of the basic methods covered, I will try to look at other types of sorting

Use SPL sorting

SplHeap

class SimpleHeapSort extends SplHeap { public function compare($a, $b) { return strcmp($a, $b); } } // Let's populate our heap here (data of 2009) $heap = new SimpleHeapSort(); $heap->insert("a"); $heap->insert("b"); $heap->insert("c"); echo implode(PHP_EOL, iterator_to_array($heap));

Output

c b a

SplMaxHeap

The SplMaxHeap class provides the main functionality of the heap, keeping the maximum value at the top.

$heap = new SplMaxHeap(); $heap->insert(1); $heap->insert(2); $heap->insert(3);

SplMinHeap

$heap = new SplMinHeap (); $heap->insert(3); $heap->insert(1); $heap->insert(2);

Other types of sorting

Bubble Sort

Excerpted fromWikipedia article about bubble sort:

function bubbleSort(array $array) { $array_size = count($array); for($i = 0; $i

Select sort

Excerpted fromWikipedia article on selection sort:

function selectionSort(array $array) { $length = count($array); for($i = 0; $i

Insertion sort

Excerpted fromWikipedia article about insertion sort:

function insertionSort(array $array) { $count = count($array); for($i = 1; $i = 0 && $array[$j] > $element ) { $array[$j + 1] = $array[$j]; $array[$j] = $element; $j = $j - 1; } } return $array; }

Hill sort

Excerpted fromWikipedia article about Shellsort:

function shellSort(array $array) { $gaps = array( 1, 2, 3, 4, 6 ); $gap = array_pop($gaps); $length = count($array); while ( $gap > 0 ) { for($i = $gap; $i = $gap && $array[$j - $gap] > $tmp ) { $array[$j] = $array[$j - $gap]; $j -= $gap; } $array[$j] = $tmp; } $gap = array_pop($gaps); } return $array; }

comb sorting

Excerpted fromWikipedia article on comb sort:

function combSort(array $array) { $gap = count($array); $swap = true; while ( $gap > 1 || $swap ) { if ($gap > 1) $gap /= 1.25; $swap = false; $i = 0; while ( $i + $gap $array[$i + $gap]) { // swapping the elements. list($array[$i], $array[$i + $gap]) = array( $array[$i + $gap], $array[$i] ); $swap = true; } $i ++; } } return $array; }

Merge sort

FromWikipedia article about merge sort:

function mergeSort(array $array) { if (count($array) 0 && count($right) > 0 ) { if ($left[0] 0 ) array_push($result, array_shift($left)); while ( count($right) > 0 ) array_push($result, array_shift($right)); return $result; }

Quick sort

Excerpted fromWikipedia article about quick sort:

function quickSort(array $array) { if (count($array) == 0) { return $array; } $pivot = $array[0]; $left = $right = array(); for($i = 1; $i

Arrange sorting

Excerpted fromWikipedia article about sorting:

function permutationSort($items, $perms = array()) { if (empty($items)) { if (inOrder($perms)) { return $perms; } } else { for($i = count($items) - 1; $i >= 0; -- $i) { $newitems = $items; $newperms = $perms; list($foo) = array_splice($newitems, $i, 1); array_unshift($newperms, $foo); $res = permutationSort($newitems, $newperms); if ($res) { return $res; } } } } function inOrder($array) { for($i = 0; $i $array[$i + 1]) { return False; } } } return True; }

Radix sort

Excerpted fromWikipedia article on radix sort:

// Radix Sort for 0 to 256 function radixSort($array) { $n = count($array); $partition = array(); for($slot = 0; $slot age & 0xFF][] = &$array[$i]; } $i = 0; for($slot = 0; $slot
    P粉952365143

    Basic one-dimensional array

    $array = array(3, 5, 2, 8);

    Applicable sorting functions:

    • Sort
    • Sort
    • Classification
    • Sort
    • natsort
    • natcasesort
    • ksort
    • krsort

    The only difference between them is whether to preserve the key-value association ("a" function), whether to sort from low to high or in reverse order ("r" >" ), whether to sort values or keys ("k") and how to compare values ("nat" vs. normal). Seehttp://php.net /manual/en/array.sorting.phpLink to get an overview and more details.

    Multidimensional arrays, including object arrays

    $array = array( array('foo' => 'bar', 'baz' => 42), array('foo' => ..., 'baz' => ...), ... );

    If you want to sort the$arrayby the key "foo" for each entry, you will need acustom comparison function. Thesortabove and related functions work well for simple values that they know how to compare and sort. PHP doesn't simply "know" what to do withcomplex valueslike array('foo' => 'bar', 'baz' => 42); so you need to tell it.

    To do this, you need to create acomparison function. This function accepts two elements and must return0if the elements are considered equal; if the first value is lower, it must return a value lower than0; if the first value is considered equal A value below0 must return a value above0if the first value is higher. This is what is needed:

    function cmp(array $a, array $b) { if ($a['foo'] $b['foo']) { return 1; } else { return 0; } }

    Typically, you need to usean anonymous functionas a callback. If you want to use methods or static methods, seeOther Ways to Specify Callbacks in PHP.

    You can then use one of the following functions:

    Again, they only differ in whether key-value associations are preserved and whether they are sorted by value or key. Please read their documentation for details.

    Usage example:

    usort($array, 'cmp');

    usortwill take two items from the array and call yourcmpfunction with them. Socmp()will callarray('foo' => 'bar', 'baz' => 42)andin the form$a$bas anotherarray('foo' => ..., 'baz' => ...). The function then returnsusortwhich value is greater or if they are equal.usortRepeat this process, passing different values for$aand$b, until the array is sorted.cmpThe function will be called multiple times,at leastas many values as there are in$array, anddifferent combinations of values each time code >$a and$b.

    To get used to this idea, try the following:

    function cmp($a, $b) { echo 'cmp called with $a:', PHP_EOL; var_dump($a); echo 'and $b:', PHP_EOL; var_dump($b); }

    All you do is define a custom way to compare two items and that's all you need. This works for a variety of values.

    BTW, this works with any values, which don't have to be complex arrays. You can also compare simple arrays of numbers if you want to do a custom comparison.

    sortSorting by reference will not return anything useful!

    Note that arraysare sortedin-place, you do not need to assign the return value to anything.$array = sort($array)will replace the array withtrue, not the sorted array. Justsort($array);.

    Custom number comparison

    If you want to sort by the numeric keysbaz, all you need to do is:

    function cmp(array $a, array $b) { return $a['baz'] - $b['baz']; }

    Thanks tothe power of math, this will return a value 0 depending on whether$ais less than, equal to, or greater than$b.

    Note that this will not work forfloatvalues, as they will be simplified tointand lose precision. Please use explicit-1,0, and1return values instead.

    Object

    If you have an array of objects, it works the same way:

    function cmp($a, $b) { return $a->baz - $b->baz; }

    function

    You can do anything you need in the comparison function, including calling functions:

    function cmp(array $a, array $b) { return someFunction($a['baz']) - someFunction($b['baz']); }

    String

    Shortcut for the first string comparison version:

    function cmp(array $a, array $b) { return strcmp($a['foo'], $b['foo']); }

    strcmpdoes exactly what is expected fromcmp, which returns-1,0, or1.

    Spaceship Operator

    PHP 7 introduces thespaceship operator, which unifies and simplifies equal/less than/greater than comparisons across types: p>

    function cmp(array $a, array $b) { return $a['foo'] $b['foo']; }

    Sort by multiple fields

    If you want to sort primarily byfoo, but if two elements'fooare equal, then sort bybaz:

    function cmp(array $a, array $b) { if (($cmp = strcmp($a['foo'], $b['foo'])) !== 0) { return $cmp; } else { return $a['baz'] - $b['baz']; } }

    For those familiar, this is equivalent to a SQL query usingORDER BY foo, baz.
    See alsothis very concise shorthand versionandhow to dynamically create such a comparison function for any number of keys.

    Sort by manual static order

    If you want to sort the elements into "manual order", e.g."foo", "bar", "baz":

    function cmp(array $a, array $b) { static $order = array('foo', 'bar', 'baz'); return array_search($a['foo'], $order) - array_search($b['foo'], $order); }

    As with all of the above, if you're using PHP 5.3 or higher (and you really should be), use anonymous functions to shorten your code and avoid another global function:

    usort($array, function (array $a, array $b) { return $a['baz'] - $b['baz']; });

    This is a simple way to sort complex multi-dimensional arrays. Again,teach PHP how to determine which of two items is "bigger"; let PHP do the actual sorting.

    Additionally, for all of the above, to switch between ascending and descending order, just swap the$aand$bparameters. For example:

    return $a['baz'] - $b['baz']; // ascending return $b['baz'] - $a['baz']; // descending

    Sort an array based on another array

    There is also a peculiararray_multisort, which allows you to sort an array based on: Another:

    $array1 = array( 4, 6, 1); $array2 = array('a', 'b', 'c');

    The expected result here is:

    $array2 = array('c', 'a', 'b'); // the sorted order of $array1

    Get there usingarray_multisort:

    array_multisort($array1, $array2);

    Starting with PHP 5.5.0, you can usearray_columnto extract a column from a multidimensional array and sort the array on that column:

    array_multisort(array_column($array, 'foo'), SORT_DESC, $array);

    You can also sort multiple columns in either direction:

    array_multisort(array_column($array, 'foo'), SORT_DESC, array_column($array, 'bar'), SORT_ASC, $array);

    Starting with PHP 7.0.0, you can also extract properties from an array of objects.


      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!