我正在努力完成我正在编写的用于清理多维数组的函数的最后一步。 我希望函数遍历数组(和任何子数组),然后返回一个清理过的数组。
虽然我可以使用 array_walk_recursive
输出清理后的数据,但我正在努力将数据作为与输入结构相同的数组返回。 谁能帮忙? 任何帮助非常感谢...
这是我的代码:
function process_data($input){
function clean_up_data($item, $key)
{
echo strip_tags($item) . ' '; // This works and outputs all the cleaned data
$key = strip_tags($item); // How do I now output as a new array??
return strip_tags($item);
}
array_walk_recursive($input, 'clean_up_data');
}
$array = process_data($array); // This would be the ideal usage
print_r($array); // Currently this outputs nothing
你可以像这样使用array_walk_recursive
:
<?php
$arr = array(...);
function clean_all($item,$key)
{
$item = strip_tags($item);
}
array_walk_recursive($arr , 'clean_all');
?>
或:
这是一个递归函数,我认为它解决了你的问题:
<?php
function clean_all($arr)
{
foreach($arr as $key=>$value)
{
if(is_array($value)) $arr[$key] = clean_all($value);
else $arr[$key] = strip_tags($value);
}
return $arr;
}
?>
你需要通过引用传递值
function clean_up_data(&$item, $key)