Monday, 9 September 2013

How to loop through and clean a recursive array (PHP)

How to loop through and clean a recursive array (PHP)

I'm struggling with the final step on a function that I am writing to
clean up a multi-dimensional array. I want the function to loop through
the array (and any sub-arrays) and then return a cleaned array.
Whilst I can use array_walk_recursive to output the cleaned data, I'm
struggling with returning the data as an array in the same structure as
the inputted. Can anyone help? Any help greatly appreciated....
Here's my code:
function process_data($input){
function clean_up_data($item, $key)
{
echo strip_tags($item) . ' '; // This works and outputs all the
cleaned data
$key = strip_tags($item); // How do I now output as a new array??
return strip_tags($item);
}
array_walk_recursive($input, 'clean_up_data');
}
$array = process_data($array); // This would be the ideal usage
print_r($array); // Currently this outputs nothing

No comments:

Post a Comment