2

我正在尝试创建一个适用于简单数组和嵌套数组的函数。到目前为止,该函数如下所示:

function fnPrepareDataForBrowser($values)
  {
    $values = array_map('htmlspecialchars', $values);   
    return $values;
  }

它适用于简单的数组 - 例如:

Array
(
    [Item ID] => 25469
    [Item Desc] => spiral nails, 1"
    [Standard Item] => yes
    [Entry UOM] => lb
)

但它失败并显示消息“警告:htmlspecialchars() 期望参数 1 是字符串,给定数组...”用于嵌套数组 - 例如:

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
    )
)

应该对函数进行哪些修改以使其适用于简单数组和嵌套数组?

4

4 回答 4

2

尝试这个:

<?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;
    }
?>
于 2013-08-01T13:00:12.250 回答
1

您可以改用此 array_map_recursive 函数。我从说明书上偷的。

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;
}
于 2013-08-01T13:01:36.580 回答
1
function fnPrepareDataForBrowser(& $values)
{
    return is_array($values) ? 
           array_map('fnPrepareDataForBrowser', $values) : 
           htmlspecialchars($values);   

}

$array = fnPrepareDataForBrowser( $your_array );
于 2013-08-01T13:13:22.023 回答
0

这符合您的需求吗?

<?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 );

产生这些结果

Array
(
    [0] => Array
        (
            [Item ID] => 25469
            [Item Description] => spiral nails &amp; 1
            [Standard Item] => yes&amp;
            [Entry UOM] => lb&gt;
        )

    [1] => Array
        (
            [Item ID] => 25470
            [Item Description] => finishing screws &amp; 2.5
            [Standard Item] => no&amp;
            [Entry UOM] => lb&lt;
        )

    [2] => Array
        (
            [Item ID] => 25576
            [Item Description] => paint brush &amp; 3
            [Standard Item] => no&amp;
            [Entry UOM] => each&amp;
        )

)
于 2013-08-01T13:26:30.573 回答