is it possible if callback in array_filter receive parameter?

前端 未结 5 1060
小蘑菇
小蘑菇 2020-12-17 16:05

I got this multiple array named $files[], which consists of keys and values as below :

[0] => Array
(
    [name] => index1.php
    [path]          


        
5条回答
  •  庸人自扰
    2020-12-17 16:26

    You can make use of the array_walk function as:

    $arr = array(
            array("name" => "Jack", "id" => 1),
            array("name" => "Rose", "id" => 2),
        );
    
    $result = array(); // initialize result array.
    array_walk($arr, "filter"); // iterate over the array calling filter fun for each value.
    // $result will now have elements "Jack" and "Rose"
    
    function filter($a) {
    
        global $result; // to access the global result array.
    
        $result[] = $a['name']; // for each array passed, save the value for the key 'name' in the result array.
    }
    

提交回复
热议问题