-1

I have an multidimensional array with these structure:

$futureInvoices[] = Array(
    "date" => date("Y-m-d", $ts),
    "customer" => $possibleDomain->userid,
    "subtotal" => "0.00",
    "total" => "0.00",
);

I just want to sort the entries in the array $futureInvoices by date ascending and then by amount descending. How is that possible?

Richard
  • 2,772
  • 3
  • 20
  • 36
  • 1
    http://php.net/manual/en/function.array-multisort.php using http://php.net/manual/en/function.array-column.php – AbraCadaver Sep 03 '15 at 18:19

1 Answers1

-2

http://php.net/manual/en/function.array-multisort.php

array_multisort

There is a specific example in the docs

<?php
$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);
?>
In this example, we will order by volume descending, edition ascending.

We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.

<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>

in your case the loop will be extracting the values of date and total. Then the call to the function will be identical

exussum
  • 16,939
  • 7
  • 29
  • 61