I am trying to move the strings that contain an underscore _
to the bottom of my array. I'm assuming usort()
is the best way but not sure how to do this in the most efficient way. Let's say my array is '1','34_1','35_1','36_1','7','41_4','38_5','5','41_5','44_5','45_5'
usort(['1','34_1','35_1','36_1','7','41_4','38_5','5','41_5','44_5','45_5'], function (){...});
UPDATE: I think I found a way to do it:
$myarray = ['1','34_1','35_1','36_1','7','41_4','38_5','5','41_5','44_5','45_5'];
function sortem($myarray) {
foreach ($myarray as $index=>$item)
{
if (preg_match('/^_+$/', $item))
{
unset($myarray[$index]);
$myarray[$index] = $item;
}
}
}
usort($myarray, "sortem");
$tagsuri = array_reverse($tagsuri);
Is there a better way?