sorting array based on child array[0] (unix) value

Related searches

I need an array sorted by Unix timestamp values. I attempted to use both ksort and krsort before realising that occasionally the timestamp values might be the same (and you cannot have duplicate keys in arrays).

Here's an example array I may be faced with:

$array = array(
        "unix"      => 1556547761, // notice the two duplicate unix values
        "random"    => 4
        "unix"      => 1556547761,
        "random"    => 2
        "unix"      => 1556547769,
        "random"    => 5
        "unix"      => 1556547765, // this should be in the 3rd position
        "random"    => 9

So what I'm trying to do is sort them all based on each child arrays unix value, however I cannot figure out how to do so. I have tried countless insane ways (including all other sort functions and many, many for loops) to figure it out - but to no avail.

All help is appreciated.

array_multisort() — Sort multiple or multi-dimensional arrays

array_columns() — Return the values from a single column in the input array

You can use array_multisort() and array_column(), then provide your desired sort order (SORT_ASC or SORT_DESC).

array_multisort(array_column($array, "unix"), SORT_ASC, $array);


In array_multisort(), arrays are sorted by the first array given. You can see we are using array_column($array, "unix"), which means that the second parameter is the order of sorting (ascending or descending) and the third parameter is the original array.

This is the result of array_column($array, "unix"):

    [0] => 1556547761
    [1] => 1556547761
    [2] => 1556547765
    [3] => 1556547769

array_multisort - Manual, as of PHP 5.5.0 you can use array_column() instead of the above code usort() - Sort an array by values using a user-defined comparison function; The comparison of array This function passes the entire child element even if it is not a string. This function sorts arrays natural multi-dimensional based on key value Another option for sorting multi-dimensional arrays is to use a built in function called array_multisort(). While it can also be used to sort several independent arrays at once, its real power comes into play because it provides an off the shelf method to sort a multi-dimensional array by one or more "child" dimensions.

You can use usort which sort your array by given function

Define function as:

function cmpByUnix($a, $b) {
    return $a["unix"] - $b["unix"];

And use with: usort($array, "cmpByUnix");

Live example: 3v4l

Notice you can also use asort($array); but this will compare also the "random" field and keep the key - if this what you need then look at Mangesh answer

asort - Manual, This function passes the entire child element even if it is not a string. output of alphanumeric with case-sensitive data sorting by values. Array ( [0] => example1 The sorting problem I was refearing to causes a character based sorting done� array_name[index]=value Here array_name is the name of the array, index is the index of the item in the array that you want to set, and value is the value you want to set for that item. As an example, the following commands − NAME[0]="Zara" NAME[1]="Qadir" NAME[2]="Mahnaz" NAME[3]="Ayan" NAME[4]="Daisy" If you are using the ksh shell, here is

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.

Note:If two members compare as equal, their relative order in the sorted array is undefined.

Refer :


echo "<pre>";
echo "</pre>";

It will give you the output as

    [1] => Array
            [unix] => 1556547761
            [random] => 2

    [0] => Array
            [unix] => 1556547761
            [random] => 4

    [3] => Array
            [unix] => 1556547765
            [random] => 9

    [2] => Array
            [unix] => 1556547769
            [random] => 5


You can keep the array key [1],[0],[3],[2]) as it is Or you can keep it as sequential as per your requirement.

sorting in fork(), Calculation in parent and child process using fork() � Sorting a Map by value in C++ STL � Sorting a vector in C++ � Sorting Vector of Arrays in C++� $ my_array[0]=foo Array operations Once an array is created, we can perform some useful operations on it, like displaying its keys and values or modifying it by appending or removing elements: Print the values of an array To display all the values of an array we can use the following shell expansion syntax: $ echo ${my_array[@]} Or even:

Quicksort, Quicksort is an efficient sorting algorithm. Developed by British computer scientist Tony Hoare The base case of the recursion is arrays of size zero or one, which are in and may not be sorted until the base case of a partition with a single element is This means that the call tree is a linear chain of n − 1 nested calls. Array elements are by default separated by one or more white spaces. * Your de-referencing of array elements is wrong. See the correct usage below, # echo ${array_name[0]} Now coming to your question: Yes, it is possible. You need to initialize the array by referencing the index as, # array_name=([1]=name_1 name_2 name_3 name_4 name_5) This means

Array — Godot Engine latest documentation, Array�. Category: Built-In Types Returns the last element of the array if the array is not empty (size>0). index that maintains sorting order, if the value is not yet present in the array) If deep is true , a deep copy is be performed: all nested arrays and dictionaries are duplicated and will not be shared with the original array� Sorting an array. Now suppose I need to sort my array. There are actually two ways to do this. The first way to do this is to use the Sort-Object cmdlet (Sort is an alias for the Sort-Object cmdlet). The second way to sort an array is to use the static Sort method from the System.Array .NET Framework class. Use Sort and the pipeline

Array in Shell Scripting An array is a systematic arrangement of the same type of data. But in Shell script Array is a variable which contains multiple values may be of same type or different type since by default in shell script everything is treated as a string. An array is zero-based ie indexing start with 0. How to Declare Array in Shell

  • Let me give it a shot! :)
  • Any chance you can explain how this works a bit better? It's very compact and works, however it lacks explanation.
  • @GROVER. I have mentioned the explanation in the solution, hope you will understand.
  • Ooh clever and clean, let me give it a shot
  • Nice of you to promote someone else's answer! But your answer is exactly what I wanted! Just out of curiosity, is there a way to flip this array using the current method without array_reverse? Or is it just easier to do it with that
  • I guess you can just change the function to $b["unix"] - $a["unix"] - notice the $b at the beginning makes it DESC. If this help you you may mark it as accepted
  • Oh doy, obviously - why didn't I think of that? Hahahaha