I am trying to create a function that works on both simple arrays and nested arrays. So far, the function looks like this:
JavaScript
x
function fnPrepareDataForBrowser($values)
{
$values = array_map('htmlspecialchars', $values);
return $values;
}
It works fine for simple arrays – for example:
JavaScript
Array
(
[Item ID] => 25469
[Item Desc] => spiral nails, 1"
[Standard Item] => yes
[Entry UOM] => lb
)
But it fails with the message “Warning: htmlspecialchars() expects parameter 1 to be string, array given…” for nested arrays – for example:
JavaScript
Array
(
[0] => Array
(
[Item ID] => 25469
[Item Description] => spiral nails, 1"
[Standard Item] => yes
[Entry UOM] => lb
)
[1] => Array
(
[Item ID] => 25470
[Item Description] => finishing screws, 2.5"
[Standard Item] => no
[Entry UOM] => lb
)
[2] => Array
(
[Item ID] => 25576
[Item Description] => paint brush, 3"
[Standard Item] => no
[Entry UOM] => each
)
)
What modifications should be made to the function so it works for both simple arrays and nested arrays?
Advertisement
Answer
JavaScript
function fnPrepareDataForBrowser(& $values)
{
return is_array($values) ?
array_map('fnPrepareDataForBrowser', $values) :
htmlspecialchars($values);
}
$array = fnPrepareDataForBrowser( $your_array );