JavaScript
x
static public function sortArrayByDate($array, $table, $fieldName){
$arr_1 = array();
foreach($array as $val_a)
{
$arr_1[] .= $val_a;
$arr_2 = array();
foreach($arr_1 as $val_b){
$obj = Doctrine_Core::getTable("$table")->findOneBy('id', $val_b);
$arr_2[] = array(
'id' => $val_b,
"$fieldName" => $obj->get($fieldName)
);
}
}
/*
* $arr_2 array result looks like:
*
* array[0]
* id = 1
* date_field = 2013-5-20 //'date_field' name may be variable
* array[1]
* id = 2
* date_field = 2012-5-20
*/
$date_compare = function($a, $b)
{
if ($a[$fieldName] == $b[$fieldName]) {
return 0;
}
return ($a[$fieldName] < $b[$fieldName]) ? -1 : 1;
};
usort($arr_2, $date_compare);
return $arr_2;
}
The problem I’m having is passing the variable field_name to the date_compare function, which can only take two variables due to the usort method. date_compare function must also be wrapped in a variable to avoid redeclaring a function if it is called more than once. m I even going about this the right way?
Advertisement
Answer
The PHP construct is use
JavaScript
$date_compare = function ($a, $b) use ($fieldName) {
}