I have an array that looks something like this:
array(
array('foo' => true),
array('foo' => false),
array('foo' => true),
array('foo' => true),
array('foo' => false)
)
Is there a simple way to bring all the arrays where foo == true
to the top?
Just use the uasort()
function to order your array:
$arr = array(
array('foo' => true),
array('foo' => false),
array('foo' => true),
array('foo' => true),
array('foo' => false)
);
function sortit($a, $b) {
if($a['foo'] === $b['foo']) {
return 0;
}
return $a['foo'] > $b['foo'] ? -1 : 1;
}
uasort($arr, 'sortit');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With