Forum Moderators: coopster

Message Too Old, No Replies

Walk through entire multidimensional array?

         

dbzfyam

4:35 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Does anyone know if it is possible to walk through an entire multidimensional array (in this case the $_POST array)? I need this to 'sanitize' it a bit (remove script tags for example). Sample array:

Array
(
[userID] => 1
[userAddress] => "street"
[userPostal] => "12345"
[userCity] => "NY"
[userCountry] => "US"
[payment] => Array
(
[type] => "mastercard"
[number] => "12345678"
[cart] => Array
(
[0] => Array
(
[ID] => 1
[amount] => 31231
[comment] => ""
)
[1] => Array
(
[ID] => 13
[score] => 15611
[comment] => ""
)
[2] => Array
(
[ID] => 15
[score] => 9151
[comment] => ""
)
)
)

I need to check all of those. I've looked at array_walk_recursive and array_walk but have no idea how to use those with a multidimensional array.

Thanks in advance,
Stefan

rodriguez1804

7:52 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



how about doing nested for loops. It may not be the most efficient way to go about it, but it should do the job.

dreamcatcher

8:47 pm on Aug 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function multiDimensionalArrayMap($func,$arr) {
$newArr = array();
if (!empty($arr)) {
foreach($arr AS $key => $value) {
$newArr[$key] = (is_array($value) ? multiDimensionalArrayMap($func,$value) : $func($value));
}
}
return $newArr;
}

$_POST = multiDimensionalArrayMap('yourFunctionHere',$_POST);

dc

dbzfyam

11:41 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Thanks for the fast replies! I just tried dc's script and it's working perfectly! I did try something like that but somehow the array got scrambled (too bad I deleted it). Thanks for the help!