I have a multiarray that has a timestamp as one of the elements and I trying to sort on that. The example below has the dates in descending order but I want them in ascending order. So the out put is [first] => Array [item1] => d_2013-04-07 [date] => 1365336914 [item2] => d_2012-09-09 [date] => 1347192912 [second] => Array [item1] => d_2013-04-07 [date] => 1365336914 [item2] => d_2012-09-09 [date] => 1347192912 Code (markup): but I need it to be [first] => Array [item2] => d_2012-09-09 [date] => 1347192912 [item1] => d_2013-04-07 [date] => 1365336914 [second] => Array [item2] => d_2012-09-09 [date] => 1347192912 [item1] => d_2013-04-07 [date] => 1365336914 Code (markup): Here's the code. <?php $arry = array(); $arry['first'] = array( array('item1' => 'd_2013-04-07', 'date' => '1365336914'), array('item2' => 'd_2012-09-09', 'date' => '1347192912')); $arry['second'] = array( array('item1' => 'd_2013-04-07', 'date' => '1365336914'), array('item2' => 'd_2012-09-09', 'date' => '1347192912')); ?> <pre> <?php print_r($arry); ?> </pre><?php function cmp($a, $b) { return strcmp($a["date"], $b["date"]); } foreach($arry as $single) { uasort($single, "cmp"); } ?> <pre> <?php print_r($arry); ?> </pre> PHP: I've tried a number of sorting functions but none seem to work. I think it may be to do with my loop where I sort. Maybe the array I sort is just a copy of the main array but, if that is it, I don't know how to set the results in the main array. Would someone take a look and point out my problem, please?
<?php $arry = array(); $arry['first'] = array( array('item1' => 'd_2013-04-07', 'date' => '1365336914'), array('item2' => 'd_2012-09-09', 'date' => '1347192912')); $arry['second'] = array( array('item1' => 'd_2013-04-07', 'date' => '1365336914'), array('item2' => 'd_2012-09-09', 'date' => '1347192912')); // Date sorting function: function sort_by_date($x, $y) { return ((int)$x['date'] > (int)$y['date']); } echo 'unSorted: ,<br>'; echo '<pre>'; print_r($arry); echo '</pre>'; foreach ($arry as $key => $value) { uasort($arry[$key], 'sort_by_date'); } echo 'Sorted: <br>'; echo '<pre>'; print_r($arry); echo '</pre>'; PHP:
strider fixed the big two errors -- you weren't actually modifying $single -- his passing the $key lets it be targeted. The second error being attempting to use strcmp to compare to integers. That first error, there's a simpler way, passing by reference... where you had: foreach ($arry as $single) just add a & before $single, and now you can make changes. <?php $arry = [ 'first' => [ [ 'id' => 'd_2013-04-07', 'date' => '1365336914' ],[ 'id' => 'd_2012-09-09', 'date' => '1347192912' ] ], 'second' => [ [ 'item1' => 'd_2013-04-07', 'date' => '1365336914' ],[ 'item2' => 'd_2012-09-09', 'date' => '1347192912' ] ] ]; echo '<pre>',print_r($arry),'</pre>'; function compareDates($a, $b) { return $a['date'] > $b['date']; } foreach($arry as &$single) { uasort($single, 'compareDates'); } echo '<pre>',print_r($arry),'</pre>'; ?> Code (markup): That's PHP 5.4+ only code, they let us do JS style arrays now.
strider fixed the big two errors -- you weren't actually modifying $single -- his passing the $key lets it be targeted. The second error being attempting to use strcmp to compare to integers. That first error, there's a simpler way, passing by reference... where you had: foreach ($arry as $single) just add a & before $single, and now you can make changes. <?php $arry = [ 'first' => [ [ 'item1 => 'd_2013-04-07', 'date' => '1365336914' ],[ 'item2' => 'd_2012-09-09', 'date' => '1347192912' ] ], 'second' => [ [ 'item1' => 'd_2013-04-07', 'date' => '1365336914' ],[ 'item2' => 'd_2012-09-09', 'date' => '1347192912' ] ] ]; echo '<pre>',print_r($arry),'</pre>'; function compareDates($a, $b) { return $a['date'] > $b['date']; } foreach($arry as &$single) { uasort($single, 'compareDates'); } echo '<pre>',print_r($arry),'</pre>'; ?> Code (markup): That's PHP 5.4+ only code, they let us do JS style arrays now.