PHP: how to perform htmlspecialchar() on an array-of-arrays?

MoneyMillions

New Member
How do I run the PHP function \[code\]htmlspecialchars()\[/code\] on an array of array objects?I have the following code:\[code\]$result_set = Array( [0] => Array ( [home_id] => 1 [address] => 4225 Nasmyth Dr [city] => Plano [state] => TX [zip] => 76798 ) [1] => Array ( [home_id] => 8 [address] => 4229 Nasmyth Dr [city] => Plano [state] => TX [zip] => 75093 ));// this doesn't work since $result_set is an array of arrays and htmlspecialchars is expecting a stringhtmlspecialchars($result_set, ENT_QUOTES, 'UTF-8')); \[/code\]UPDATE:Please note that even though there are quite a few answers below, none of them work for an array-of-arrays. The answers below only work for simple arrays.I've tried the following, but it doesn't work:\[code\]array_walk_recursive($result_set, "htmlspecialchars", array(ENT_QUOTES,'UTF-8'))\[/code\]I get the following error: \[code\]htmlspecialchars() expects parameter 2 to be long, string given\[/code\]UPDATE 2When I try:\[code\]function cleanOutput(&$value) { return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');}print_r($result_set);print('-------');print_r(array_walk_recursive($result_set, "cleanOutput"));\[/code\]I get the following, undesired, output:\[code\]Array( [0] => Array ( [home_id] => 1 [address] => 4225 Nasmyth Dr [city] => Plano [state] => TX [zip] => 76798 ) [1] => Array ( [home_id] => 8 [address] => 4229 Nasmyth Dr [city] => Plano [state] => TX [zip] => 75093 ))-------1\[/code\]UPDATE 3When I try:\[code\]function cleanOutput(&$value) { return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');}$result_set = Array ( [0] => Array ( [home_id] => 1 [address] => 4225 Nasmyth Dr [city] => Plano [state] => TX [zip] => 76798 ) [1] => Array ( [home_id] => 8 [address] => 4229 Nasmyth Dr [city] => Plano [state] => TX [zip] => 75093 ) );$cleanedOutput = array();foreach ($result_set as $rs) { $cleaned[] = array_map("cleanOutput", $rs);}print_r($cleanedOutput);\[/code\]I get the following, undesired, results:\[code\]{'homes' : []}\[/code\]
 
Back
Top