duai4512 2013-09-09 10:17
浏览 103
已采纳

如何循环并清理递归数组(PHP)

I'm struggling with the final step on a function that I am writing to clean up a multi-dimensional array. I want the function to loop through the array (and any sub-arrays) and then return a cleaned array.

Whilst I can use array_walk_recursive to output the cleaned data, I'm struggling with returning the data as an array in the same structure as the inputted. Can anyone help? Any help greatly appreciated....

Here's my code:

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
  • 写回答

2条回答 默认 最新

  • donglankui1263 2013-09-09 10:24
    关注

    you can use array_walk_recursive like that :

    <?php
    $arr = array(...);
    function clean_all($item,$key)
    {
    $item = strip_tags($item);
    }
    array_walk_recursive($arr , 'clean_all');
    ?>
    

    OR :

    this is a recursive function , i think it solves your problem :

    <?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;
        }
    
         ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题