简体   繁体   中英

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.

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. Any help is appreciated.

You could simply make use of array_walk_recursive :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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