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

PHP Cleaning up nested arrays with unknown structure

假设我有一个这样的数组:

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

数组有一个 "unorganized" 结构,子数组有其他值。

我想 运行 对每个值执行 htmlentities 函数,以确保值中没有任何不良内容。

我一直在阅读 RecursiveIteratorIterator,但我找不到如何使用它来将 函数 应用于相当随机嵌套中的每个值的示例多维数组。任何帮助表示赞赏。

你可以简单地使用 array_walk_recursive:

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

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