Reputation: 2768
I am trying to create a function that works on both simple arrays and nested arrays. So far, the function looks like this:
function fnPrepareDataForBrowser($values)
{
$values = array_map('htmlspecialchars', $values);
return $values;
}
It works fine for simple arrays - for example:
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:
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?
Upvotes: 2
Views: 246
Reputation: 94672
Does this suit your needs?
<?php
function fnPrepareDataForBrowser(&$values)
{
$result = array();
foreach ( $values as $k => $v )
{
if ( is_array( $v ) ) {
array_push( $result, array_map('htmlspecialchars', $v) );
}
}
return $result;
}
$tst = array(
array (
'Item ID' => 25469,
'Item Description' => "spiral nails & 1",
'Standard Item' => 'yes&',
'Entry UOM' => 'lb>'
),
array (
'Item ID' => 25470,
'Item Description' => "finishing screws & 2.5",
'Standard Item' => 'no&',
'Entry UOM' => 'lb<'
),
array (
'Item ID' => 25576,
'Item Description' => "paint brush & 3",
'Standard Item' => 'no&',
'Entry UOM' => 'each&'
)
);
$result = fnPrepareDataForBrowser($tst);
print_r( $result );
Produces these results
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&
)
)
Upvotes: 0
Reputation: 4656
function fnPrepareDataForBrowser(& $values)
{
return is_array($values) ?
array_map('fnPrepareDataForBrowser', $values) :
htmlspecialchars($values);
}
$array = fnPrepareDataForBrowser( $your_array );
Upvotes: 1
Reputation: 32921
You can use this array_map_recursive function instead. I stole it from the manual.
function array_map_recursive($callback, $array) {
foreach ($array as $key => $value) {
if (is_array($array[$key])) {
$array[$key] = array_map_recursive($callback, $array[$key]);
}
else {
$array[$key] = call_user_func($callback, $array[$key]);
}
}
return $array;
}
Upvotes: 1
Reputation: 17018
Try this:
<?php
/**
* Applies callback function recursively to every element of the given array.
*
* If the array contains inner arrays or objects, the callback is also applied
* to these.
*
* Caution: If $arrayOfObject is an object, only public members are processed.
*
* @param callback $func
* @param array|object $array
*
* @return array
*/
public static function array_map_recursive($func, $arrayOrObject)
{
$array = is_array($arrayOrObject) ? $arrayOrObject : get_object_vars($arrayOrObject);
foreach ($array as $key => $val) {
$array[$key] = is_array($val) || is_object($val)
? self::array_map_recursive($func, $val)
: call_user_func($func, $val);
}
return $array;
}
?>
Upvotes: 2