How to use array_filter to add closure function Filter Array by PHP

$arr = array(
    array(
        "catid" => 2,
        "catdir" => "notice",
    ),
    array(
        "catid" => 5,
        "catdir" => "subject",
    ),
    array(
        "catid" => 6,
        "catdir" => "news"
    ),
);

$catid = 5;
$res = array_filter($arr, function($param) use ($catid) {
    return $param["catid"] == $catid;
});
print_r($res);

my current code outputs the following secondary array

array(1) {
  [1]=>
  array(2) {
    ["catid"]=>
    int(5)
    ["catdir"]=>
    string(7) "subject"
  }
}

I want to output the following format

array(2) {
    ["catid"]=>
    int(5)
    ["catdir"]=>
    string(7) "subject"
}

in addition, I don"t quite understand closure functions, and there are the following questions: what are the variables
1, $param and $catid used
2, and what is use used here

Php
Jul.15,2021

$catid = 5;
array_filter ($arr, function ($param) use ($catid, &$array) {
    ($param['catid'] == $catid) ? $array = $param : $array = [];
});
    
var_dump ($array);

you can do this to achieve the output

array(2) {
    ["catid"]=>
    int(5)
    ["catdir"]=>
    string(7) "subject"
}
The result of

,
1, $param and $catid
$param is used to store the results of each loop, $catid is the value you need to query
2, and what is use used here
you can regard the parameters in use as arguments, variables that can communicate with the outside world.


$res = current($res);
The function of

use is to inherit the variable . To keep it simple is to use external variables. You can define a method by passing parameters, but if you use an anonymous method, you will not be able to pass parameters. Use use , which is equivalent to passing parameters

.
For the use of

closures, you can take a look at the instructions in the manual

http://php.net/manual/zh/func.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35dbf-4d666.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35dbf-4d666.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?