【问题标题】:create a PHP function that works for simple arrays and nested arrays创建一个适用于简单数组和嵌套数组的 PHP 函数
【发布时间】:2013-08-02 09:55:47
【问题描述】:

我正在尝试创建一个适用于简单数组和嵌套数组的函数。到目前为止,函数看起来是这样的:

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

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

【问题讨论】:

    标签: php arrays jagged-arrays


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:
      function fnPrepareDataForBrowser(& $values)
      {
          return is_array($values) ? 
                 array_map('fnPrepareDataForBrowser', $values) : 
                 htmlspecialchars($values);   
      
      }
      
      $array = fnPrepareDataForBrowser( $your_array );
      

      【讨论】:

        【解决方案3】:

        您可以改用这个 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;
        }
        

        【讨论】:

          【解决方案4】:

          这符合您的需要吗?

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-03
            • 1970-01-01
            • 1970-01-01
            • 2012-05-05
            • 2020-06-26
            • 1970-01-01
            • 1970-01-01
            • 2020-11-01
            相关资源
            最近更新 更多