简体   繁体   English

PHP清理结构未知的嵌套数组

[英]PHP Cleaning up nested arrays with unknown structure

Let's assume I have an array like so: 假设我有一个像这样的数组:

[1=>[1=>2,2=>"something"],2=>[1,2],3=>"hello"]

The array has a "unorganized" structure with subarrays other values. 该数组具有“无序”结构,其子数组具有其他值。

I want to run a htmlentities function on each value to make sure nothing bad is inside the values. 我想在每个值上运行htmlentities函数,以确保值内没有任何不好的地方。

I've been reading up on RecursiveIteratorIterator but I cannot find an example of how to use it to apply a function to each value in a quite random nested multidimensional array. 我一直在阅读RecursiveIteratorIterator但找不到如何使用它将函数应用于随机嵌套的多维数组中的每个值的示例。 Any help is appreciated. 任何帮助表示赞赏。

You could simply make use of array_walk_recursive : 您可以简单地使用array_walk_recursive

array_walk_recursive($input, function (&$value) {
  $value = htmlentities($value);
});

Demo: https://3v4l.org/QmRJr 演示: https//3v4l.org/QmRJr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM