Avión
Avión

Reputation: 8396

Reordeing an array by date mantaining the index in PHP

Having the following array:

array(2) {
  [1]=>   // <--------------- NOTICE THIS INDEX
  array(4) {
    [0]=>
    array(2) {
      [0]=>
      string(4) "2012"
      [1]=>
      float(1)
    }
    [1]=>
    array(2) {
      [0]=>
      string(4) "2013"
      [1]=>
      float(1)
    }
    [2]=>
    array(2) {
      [0]=>
      string(4) "2014"
      [1]=>
      float(2)
    }
    [3]=>
    array(2) {
      [0]=>
      string(4) "2015"
      [1]=>
      float(1)
    }
  }
  [2]=>  // <--------------- NOTICE THIS INDEX
  &array(7) {
    [0]=>
    array(2) {
      [0]=>
      string(4) "2009"
      [1]=>
      float(7)
    }
    [1]=>
    array(2) {
      [0]=>
      string(4) "2010"
      [1]=>
      float(2)
    }
    [2]=>
    array(2) {
      [0]=>
      string(4) "2011"
      [1]=>
      float(2)
    }
    [3]=>
    array(2) {
      [0]=>
      string(4) "2012"
      [1]=>
      float(0)
    }
    [4]=>
    array(2) {
      [0]=>
      string(4) "2013"
      [1]=>
      float(19)
    }
    [5]=>
    array(2) {
      [0]=>
      string(4) "2014"
      [1]=>
      float(8)
    }
    [6]=>
    array(2) {
      [0]=>
      string(4) "2015"
      [1]=>
      float(8)
    }
  }
}

I want to reorder the array with the oldest years first, this means in the example above, as the array with index 2 has the 2009 and the oldest year of the array with index 1 is 2012, has to change the positions. My desired output should be:

array(2) {
  [2]=>
  &array(7) {
    [0]=>
    array(2) {
      [0]=>
      string(4) "2009"
      [1]=>
      float(7)
    }
    [1]=>
    array(2) {
      [0]=>
      string(4) "2010"
      [1]=>
      float(2)
    }
    [2]=>
    array(2) {
      [0]=>
      string(4) "2011"
      [1]=>
      float(2)
    }
    [3]=>
    array(2) {
      [0]=>
      string(4) "2012"
      [1]=>
      float(0)
    }
    [4]=>
    array(2) {
      [0]=>
      string(4) "2013"
      [1]=>
      float(19)
    }
    [5]=>
    array(2) {
      [0]=>
      string(4) "2014"
      [1]=>
      float(8)
    }
    [6]=>
    array(2) {
      [0]=>
      string(4) "2015"
      [1]=>
      float(8)
    }
  }
  [1]=>
  array(4) {
    [0]=>
    array(2) {
      [0]=>
      string(4) "2012"
      [1]=>
      float(1)
    }
    [1]=>
    array(2) {
      [0]=>
      string(4) "2013"
      [1]=>
      float(1)
    }
    [2]=>
    array(2) {
      [0]=>
      string(4) "2014"
      [1]=>
      float(2)
    }
    [3]=>
    array(2) {
      [0]=>
      string(4) "2015"
      [1]=>
      float(1)
    }
  }
}

The problem here is that I'm using the following function to reorder by dates (being arraySeries the array):

$years = array();
foreach ($arraySeries as $key => $row)
{
    $years[$key] = $row[0];
}
array_multisort($years, SORT_ASC, $arraySeries);

It's working correctly, but the indexes are changed

array(2) {
  [0]=> // <-------- HERE IS A 0 WHEN IT SHOULD BE 2
  &array(7) {
    [0]=>
    array(2) {
      [0]=>
      string(4) "2009"
      [1]=>
      float(7)
    }
    [1]=>
    array(2) {
      [0]=>
      string(4) "2010"
      [1]=>
      float(2)
    }
    [2]=>
    array(2) {
      [0]=>
      string(4) "2011"
      [1]=>
      float(2)
    }
    [3]=>
    array(2) {
      [0]=>
      string(4) "2012"
      [1]=>
      float(0)
    }
    [4]=>
    array(2) {
      [0]=>
      string(4) "2013"
      [1]=>
      float(19)
    }
    [5]=>
    array(2) {
      [0]=>
      string(4) "2014"
      [1]=>
      float(8)
    }
    [6]=>
    array(2) {
      [0]=>
      string(4) "2015"
      [1]=>
      float(8)
    }
  }
  [1]=> // <------------ THIS ONE IS 1 BY PURE LUCK
  array(4) {
    [0]=>
    array(2) {
      [0]=>
      string(4) "2012"
      [1]=>
      float(1)
    }
    [1]=>
    array(2) {
      [0]=>
      string(4) "2013"
      [1]=>
      float(1)
    }
    [2]=>
    array(2) {
      [0]=>
      string(4) "2014"
      [1]=>
      float(2)
    }
    [3]=>
    array(2) {
      [0]=>
      string(4) "2015"
      [1]=>
      float(1)
    }
  }
}

How can I reorder my array by date without using array_multisort? the problem is that as the array_multisort docs states:

Note: String keys will be maintained, but numeric keys will be re-indexed, starting at 0 and increase by 1.

Should I use usort here? This is just an example of two items, but I can get a lot of items to be reordered.

Thanks in advance.

Upvotes: 2

Views: 72

Answers (2)

Stephan
Stephan

Reputation: 710

Assuming that the years in each of the sub-arrays are already sorted as in your example your uasort() would look like this:

<?php

//Definition of the custom sort function
function my_sort($a,$b)
{
    //By using $a[0][0] you access the the first year entry of your subarray. It requires the sub-array to be sorted accordingly.
    if ($a[0][0] == $b[0][0]) return 0;
    return ( (int) $a[0][0] < (int) $b[0][0] ) ? -1 : 1;
}

//Setting up the array
$arr = array(
  1=>array(
        array("2012",(float) 1),
        array("2013",(float) 1),
        array("2014",(float) 2),
        array("2015",(float) 1)
        ),
  2=>array(
        array("2009",(float) 7),
        array("2010",(float) 2),
        array("2011",(float) 2),
        array("2012",(float) 0),
        array("2013",(float) 19),
        array("2014",(float) 8),
        array("2015",(float) 8)     
        ),

  );

//calling the sort function
uasort($arr,"my_sort");

//output
var_dump($arr);

?>

If the sub-arrays are not sorted you would need to loop through the array first to sort them or modify the my_sort() function to find the oldest year in the subarray instead of simply using $a[0][0].

Upvotes: 2

Ihor
Ihor

Reputation: 297

Use uasort in order to keep index:

$years = [
  [2013, 2015],
  [2009, 2012]
];

uasort($years, function($a, $b) {
  return min($a) - min($b);
});

Upvotes: 0

Related Questions