如何迭代n深度数组以过滤字符串节点-PHP [英] How to iterate a n-deep array to filter string nodes - PHP

查看:67
本文介绍了如何迭代n深度数组以过滤字符串节点-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在字符串节点上迭代多维数组和过滤器?我正在尝试创建一种简单的方法来清理通过POST进入我的应用程序的数据,并认为这真的很方便.

How can I iterate a multidimensional array and filter on string nodes? I'm trying to create an easy way to sanitize data coming into my application via POST and think this would be really handy.

推荐答案

您可以使用递归函数遍历数组并过滤其字符串成分.例如:

You can use a recursive function to traverse the array and filter its string components. For example:

function doFilter($arr) {
    foreach ($arr as $key => $value) {
        if (is_string($value)) {
            $arr[$Key] = sanitize($value);
        } else if (is_array($value)) {
            $arr[$key] = doFilter($value);
        }
    }
    return $arr;
}

function sanitize($str) {
    // Perform necessary steps to sanitize $str
    return $str;
}

这篇关于如何迭代n深度数组以过滤字符串节点-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆