如何获得一个类似于 array_walk 的 PHP 函数来 return 一个数组?

How to get a PHP function alike array_walk that will return an array?

PHP 中是否存在任何类似于 array_walk() 的内置函数 return 数组而不是 true 或 false?

有关信息,我正在尝试以下代码,但由于代码在字符串末尾得到 OR,我需要删除它,因此我需要一个替代方法

$request_data = "Bablu"; //userdata 

$presentable_cols = array('id'=>'13141203051','name'=>'Bablu Ahmed','program'=>'B.Sc. in CSE', 'country'=>'Bangladesh');

function myfunction($value,$key,$request_data)
{
    echo " $key LIKE '% $request_data %' OR";
}
array_walk($presentable_cols,"myfunction", $request_data);

代码结果:

id LIKE '% Bablu %' OR name LIKE '% Bablu %' OR program LIKE '% Bablu %' OR country LIKE '% Bablu %' OR

The use keyword allows you to introduce local variables into the local scope of an anonymous function. This is useful in the case where you pass the anonymous function to some other function which you have no control over.

无法使用 array_map,因为这不适用于按键 (PHP's array_map including keys)。这是一个可能的解决方案:

$request_data = "Bablu"; //userdata 

$presentable_cols = array('id'=>'13141203051','name'=>'Bablu Ahmed','program'=>'B.Sc. in CSE', 'country'=>'Bangladesh');

$my_arr = [];
$myfunction = function($value,$key) use ($request_data,&$my_arr)
{
    array_push($my_arr," $key LIKE '% $request_data %'");
};

array_walk($presentable_cols,$myfunction);

echo implode("OR",$my_arr);

不要把事情复杂化。只需使用 foreach 修改您的数组或创建一个新数组:

foreach ($presentable_cols as $key => $value) {
    $presentable_cols[$key] = "$key LIKE '% $request_data %'";
}

顺便说一句,一定要消毒 $request_data