php - Sort an array according to a set of values from another array -
i aware there another question on supposed answer same thing. problem don't see array merge has do anything. don't want merge arrays , don't understand how ordering them ... don't understand ordering coming it.
if relevant please explain in bit more detail whether other answer work me or not , how
here have ( array quite large simplification )
essentially have this
array ( [0] => stdclass object ( [term_id] => 72 [name] => name [slug] => slug [term_group] => 0 [term_order] => 1 [term_taxonomy_id] => 73 [taxonomy] => gallery_category [description] => description [parent] => 78 [count] => 85 ) [1] => stdclass object ( [term_id] => 77 [name] => name [slug] => slug etc, etc, etc, there lot of objects in array
then have ordering array like
array ( [0] => 77, [1] => 72, etc
so want impose ordering of second array on first 1 - ordering array holds value of [term_id] second array in corrrect order. in example above mean reverse order of first 2 objects.
$order_array = [77, 72]; $order_array = array_flip($order_array); usort($objects, function($a, $b) use ($order_array) { return $order_array[$a->term_id] - $order_array[$b->term_id]; });
this assumes $order_array
has entry every term_id
.
Comments
Post a Comment