How to advance an element in an array to the first position

$a = [
        ["userid"=>"2",name="z"],
        ["userid"=>"1",name="l"],
        ["userid"=>"4",name="w"],
        ["userid"=>"3",name="h"],
];

how to advance userid= > 4, this line to the first position. Become

$a = [
        ["userid"=>"4",name="w"],
        ["userid"=>"2",name="z"],
        ["userid"=>"1",name="l"],
        ["userid"=>"3",name="h"],
];
What

wants to achieve is that the selected item is put first, and the foreground list is too long for the user to see which one has been selected.

find someone else to write, he seems to take the last one to the front, it doesn"t seem to work.

$a = array ("averse," baked,"c");
array_unshift ($a, array_pop ($a));
var_dump ($a); die;

)
Php
Jun.20,2021

//  
function toFirst($data, $userid = 0) {
    foreach($data as $key =>  $item) {
        if ($item['userid'] == $userid) {
            unset($data[$key]);
            array_unshift($data, $item);
            break;
        }
    }
    return $data;
}

using usort may send a little

.
function toFirst2($data, $now = []) {
    usort($data, function($a, $b) use ($now) {
        if (in_array($a['userid'], $now) && in_array($b['userid'], $now) ||
        !in_array($a['userid'], $now) && !in_array($b['userid'], $now)) {
            return 0;
        }
        return in_array($a['userid'], $now) ? -1 : 1;
    });
    return $data;
}

$a = [

['userid'=>'1','name'=>'l'],
['userid'=>'2','name'=>'z'],
['userid'=>'4','name'=>'w'],
['userid'=>'3','name'=>'h'],

];

foreach ($an as $key= > $val) {

if($val['userid'] == 4){
    $info = $val;
    unset($a[$key]);
}

}
array_unshift ($a, $info);
echo'

';<br>print_r($a);

<hr class="answer">
$a = [
    ['userid'=>'2','name' => 'z'],
    ['userid'=>'1','name' => 'l'],
    ['userid'=>'4','name' => 'w'],
    ['userid'=>'3','name' => 'h'],
];
$userid = 4;
usort($a,
    function($a, $b) use ($userid)
    {
        return $b['userid'] == $userid;
    });
var_dump($a);
Menu