Can't get sort to work on multiarray

Discussion in 'PHP' started by patter, Jun 8, 2013.

  1. #1
    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?
     
    patter, Jun 8, 2013 IP
  2. Strider64

    Strider64 Member

    Messages:
    40
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    25
    #2
    <?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:
     
    Last edited: Jun 8, 2013
    Strider64, Jun 8, 2013 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    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.
     
    deathshadow, Jun 9, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    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.
     
    deathshadow, Jun 9, 2013 IP
  5. patter

    patter Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Thank you both for very much the replies. My code is working now. :)
     
    patter, Jun 10, 2013 IP