Php save sorted array

Mastering PHP arrays: Array sorting

Sorting is an operation that arranges data in a specified way. PHP has several functions that deal with sorting arrays. Let’s examine these functions.

Good overview of sorting functions can be found in PHP manual: Sorting Arrays. All sorting functions act directly on an array variable itself because the variable is passed to sort function by reference, as opposed to returning a new sorted array. Because only variables can be passed by reference, you will get Fatal error when you try to pass array directly as a function argument.

Sort functions return true on success or false and warning message when parameter type differs from array.

 1php  2  3$array = [14, 1, 22, 8, 7];  4$return = sort($array);  5var_dump($return); // bool(true)  6  7$string = 'bar';  8$return = sort($string);  9Warning: sort() expects parameter 1 to be array, string given in php shell code on line 1 10var_dump($return); // bool(false) 

If two array members are evaluated as equal then the order is undefined. It’s unknown which number 1 will be at $array[0] or $array[1] :

1php 2 3$array = [3, 1, 2, 1]; 4sort($array); 

Most PHP sorting functions uses implementation of Quicksort.

Sorting single arrays

The simplest of sorting functions is sort, which sorts an array by its values from lowest to highest and doesn’t maintain the key association.

Modified sort functions offer also sorting by key (ksort), key-value association (asort), reverse sorting (rsort), user-defined comparison function (usort) and different variations of these functions. (arsort, krsort, uksort, ursort). There’s no reverse sorting version of user-defined sorting functions because reverse sorting can be performed by updating comparison rules.

Be careful when sorting arrays with mixed types of values because the sort function can produce unpredictable results. This also applies to sort functions with sort flag different to SORT_REGULAR, which is default sort flag for most sorting functions.

Example array with various types of scalar values:

 1php  2  3$array = [  4 true,  5 false,  6 'Hello',  7 'hi',  8 50,  9 0.25, 10 '4', 11 'hello', 12 '045', 13 '03', 14 'ABC', 15 "\x41", // hexadecimal ASCII code for letter 'A' 16 0x17, // hexadecimal representation of number 23 17 020, // octal representation of number 16 18 ]; 

Output of the sort($array) function for various sort flags will be:

Different sort flags for sort function

As you can see sorted array differs for each sort flag. Comparison of sorted keys and values is case sensitive by default. For case-insensitive comparison, SORT_FLAG_CASE combination with SORT_STRING or SORT_NATURAL can be used.

1php 2 3$oranges = ['Orange1', 'orange3', 'orange1','Orange4']; 4 5sort($oranges, SORT_STRING); 6var_dump($oranges); 7 8sort($oranges, SORT_STRING | SORT_FLAG_CASE); 9var_dump($oranges); 

Sorting with flag case

For natural ordering the natsort function and for the case-insensitive version the natcasesort function can be used. These functions are shortcuts for sort function with SORT_FLAG_CASE for case-sensitive version and SORT_FLAG_CASE | SORT_FLAG_CASE for case insensitive version. Natural ordering is using Natural Order String Comparison which is more human-friendly then a byte-by-byte comparison of sort function.

Other sorting options

Sort functions, with user-defined comparison function, give you an option to create a custom comparison function. User-defined comparison function sort family accept callback functions as a parameter. Callback functions can be simple functions, but also object methods, including static class methods and anonymous functions.

Sort function expects integer as a return value and all returning non-integer values are cast to an integer.

This behaviour is called type juggling and you can read more about it in PHP Manual: Type Juggling

Expected compare function return values:

  • $first_compared_value == $second_compared_value => integer equal to zero; 0
  • $first_compared_value < $second_compared_value =>integer less then zero; e.g -1
  • $first_compared_value > $second_compared_value => integer greater than zero; e.g 1

Example: Interpolation of sort function with SORT_STRING sort flag using anonymous function/closure.

1php 2 3$array = ['Hello', 'Ahoy','Hi', 'hello']; 4 5usort($array, function($a, $b)  6 return strcmp($a, $b); 7>); 8 9var_dump($array); 

The output of this script will be:

User sorting - SORT_STRING interpolation

It is possible to create a comparison function like this, because strcmp() function returns values as an usort() expectation defined earlier ( < 0 if str1 is less than str2; >0 if str1 is greater than str2, and 0 if they are equal).

Example: Sort by occurrence of character ‘o’:

 1php  2  3$array = ['Hello World', 'Ahoy','Hi'];  4  5function occurrenceOfO($a, $b)  6  7 $a_o_count = substr_count($a, 'o');  8 $b_o_count = substr_count($b, 'o');  9 10 if ($a_o_count === $b_o_count)  11 return 0; 12 > 13 14 return $a_o_count >= $b_o_count; 15> 16 17usort($array, 'occurrenceOfO'); 18var_dump($array); 

This outputs the following:

User sorting - Occurrence of letter o

As you can see the usort function gives you an option to create complex sorting functions. Note that this function doesn’t maintain key-value association and assigns new keys to elements. If you want to maintain association use uasort function instead.

PHP 7 introduced three-way comparison operator ( ), also known as the “spaceship operator”, which has similar behaviour to the strcmp() function. This makes easier to write callbacks for comparison functions and return correct values expected by this function.

Example of spaceship operator behaviour:

1php 2 3echo 1 1; // 0 4 echo 1 2; // -1 5 echo 2 1; // 1 

Sorting multi-arrays

For sorting multiple or multi-dimensional arrays the array_multisort function can be used. This function works more like database style sorting — ‘ORDER BY’ multiple columns statement in SQL query, rather than a sort function which excepts multiple arrays and sorts these arrays independently.

 1php  2  3$magazine[] = ['year' => 2015, 'month' => 1, 'issue' => 1];  4$magazine[] = ['year' => 2014, 'month' => 12, 'issue' => 4];  5$magazine[] = ['year' => 2015, 'month' => 2, 'issue' => 2];  6$magazine[] = ['year' => 2015, 'month' => 3, 'issue' => 3];  7$magazine[] = ['year' => 2015, 'month' => 1, 'issue' => 2];  8  9var_dump($magazine); 10array_multisort($magazine); 11var_dump($magazine); 

The output of this script will be:

Multi-array sorting

As you can see from outputted data, the array was sorted by year at first place, then by month and an issue number at last place.

Array shuffling

For randomizing the order of elements in an array the shuffle function can be used. This function doesn’t preserve key-value association and PHP doesn’t provide a built-in function for this.

The output of this script will differ on each call:

1php 2 3$numbers = [1, 2, 3, 4, 5]; 4shuffle($numbers); 5var_dump($numbers); 

Источник

Php php save sorted array in variable

The sorting occurs according to «human order» by means of using strnatcmp(), so the values appear in ascending order as one would expect. Assuming the variables you are using are declared and have assigned values, you can use it like this: Example: https://3v4l.org/8jnCe

PHP sort array with three variables

You need array_keys of this array after sorted with uasort(). The comparison function(closure) should be easy to write.

You should use usort() which allows you to use user-defined comarison function for sorting. So you have to implement function which compares the neme fields and return -1,0 or 1 value depending on the result (less then, equals or greater then). This function may use regex to get three values from name strng field and then compare them using your conditions.

 "60 to 90 in 6", "timing" => 4.7 ); $acceleration[1]= array("name" => "40 to 120 in KD", "timing" => 3.3 ); $acceleration[2]= array("name" => "60 to 100 in 4", "timing" => 1.5 ); $acceleration[3]= array("name" => "60 to 90 in 4", "timing" => 2.4 ); $acceleration[4]= array("name" => "140 to 160 in 6", "timing" => 2.4 ); $acceleration[5]= array("name" => "60 to 90 in KD", "timing" => 5.7 ); function comp_maker( $key ) < return function ($a, $b) use ( $key ) < // closure for uasort() to use return strnatcmp( $a[$key], $b[$key] ); // comp by "natural order" >; > uasort($acceleration, comp_maker( 'name' ) ); // display sorted array's keys $str = ''; foreach ( $acceleration as $key => $value ) < $str .= ", $key"; >echo substr($str,1); // output: 1, 3, 0, 5, 2, 4 

You may also look at the code here

This solution follows the sage advice about applying uasort() which is more appropriate than usort() since it preserves the array’s key-value associations. Without these being maintained, the results would be: 0,1,2,3,4,5 which conceal how the original array was reordered.

Note that the second parameter for uasort() will evaluate as a closure as a result of invoking the comp_maker() function. uasort() in turn will invoke the closure so that it may perform the comparison. The sorting occurs according to «human order» by means of using strnatcmp(), so the values appear in ascending order as one would expect.

To obtain the results, I could have used array_keys() but chose instead to avoid the overhead of a function call by iterating through the array using key,value syntax to output the new order of the sorted array’s keys.

How to sort date array in php?, If the date is in «Mysql» format ( Y-m-d or Y-m-d H:i:s ), then you can sort your array right away, no special action needed: $arr = [«2019-11-11»,

PHP — Sort Array by Value

Using a variable to sort multidimensional php array (usort) [duplicate]

That’s due to variable scope. $sortVariable is not available inside of your function. To make it available to your closure use the use language construct:

$sortVariable='content_id'; usort($list, function($a, $b) use ($sortVariable) < return $a[$sortVariable] $b[$sortVariable]; >); 

Saving both variable name and content into associative array in php

You can use compact() to accomplish this.

Assuming the variables you are using are declared and have assigned values, you can use it like this:

$ar_var = compact('name', 'id', 'start_date', 'end_date', 'status', 'details'); 

Php sort array by value ascending Code Example, array sort in php? in php sort associative arrays in descending order, according to the value which sorting function is used. Write a PHP class that will

Источник

Читайте также:  Html программы создание редактор
Оцените статью