I have this code:
function cmp_asc($a, $b){
$ta = date_create_from_format('Y/m/d', $a['props']['t']); // Y/n/j si no tienen 0 inicial
$tb = date_create_from_format('Y/m/d', $b['props']['t']);
$interval = date_diff($ta, $tb);
if($interval->days != 0){
if($interval->invert == 1){
return 1;
}else{
return -1;
}
}else{
return 0;
}
}
$arr1 = array(
'props' => array('t' => '2012/05/20')
);
$arr2 = array(
'props' => array('t' => '2012/05/21')
);
$arr3 = array(
'props' => array('t' => '2012/04/14')
);
$arr = array($arr1, $arr2, $arr3);
uasort($arr, 'cmp_asc');
and I’d like to know if I can pass the ['props']['t'] via arguments.
So, it will end up like:
function cmp_asc($a, $b, $key){
$ta = date_create_from_format('Y/m/d', $a <-- $key -->);
...
...
I’m thinking about variable variables, but I’m not too sure this is the right way to do it.
Any other ideas?
Not really as argument, because you’re not calling the function yourself, but you can use closures to pass other variables into the function:
That’s just a simple example. If you need dynamic key depths, this’ll need a bit more code, along these lines:
Another idea would be using a class, usage of which would look like this:
I’ll let you figure out the implementation of
ComparisonClass.A nicer solution may be to simply standardize the format of the array you’re going to sort.