How do I sort a multidimensional array by a value, but with a custom order?
The multidimensional array will be something like:
$attribute_content[0]['term_id'] = 76;
$attribute_content[0]['content'] = "ratio";
$attribute_content[1]['term_id'] = 18;
$attribute_content[1]['content'] = "ideal condition";
$attribute_content[2]['term_id'] = 164;
$attribute_content[2]['content'] = "genotype";
$attribute_content[3]['term_id'] = 218;
$attribute_content[3]['content'] = "genetics";
$attribute_content[4]['term_id'] = 60;
$attribute_content[4]['content'] = "height";
and the ordering is according to the term_id, but will be:
$order = array(18,164,218,60,76);
I've tried the following codes which re-order the array, but seemingly randomly:
usort($attribute_content, function ($a, $b) use ($order) {
$pos_a = array_search($a, $order);
$pos_b = array_search($b, $order);
return $pos_a - $pos_b;
});
and
$weights = array_flip($order);
usort($attribute_content, function($x, $y) use($weights) {
return $weights[$x['term_id']] - $weights[$y['term_id']];
});
Please help