致命错误:调用CI控制器解决方案的未定义函数array_replace_recurive()


Fatal error: Call to undefined function array_replace_recursive() for CI Controller solution

我得到一个错误

致命错误:调用未定义的函数array_replace_recurive()

因为我的php版本不够高。因此,我搜索运行array_replace_recursive()到当前PHP版本的解决方案或替代方案。

我使用Codeigniter控制器。

这是我的代码,我希望它能有所帮助。

<?php
        function paginator($options = array() ) {
            $keepLive = '';
            $sort   = (!is_null($this->input->get('sort', true)) ? $this->input->get('sort', true) : '');
            $order  = (!is_null($this->input->get('order', true)) ? $this->input->get('order', true) : '');
            $rows   = (!is_null($this->input->get('rows', true)) ? $this->input->get('rows', true) : '');
            $search     = (!is_null($this->input->get('search', true)) ? $this->input->get('search', true) : '');
            $appends = array();
            if($sort!='')   $keepLive .='&sort='.$sort;
            if($order!='')  $keepLive .='&order='.$order;
            if($rows!='')   $keepLive .='&rows='.$rows;
            if($search!='') $keepLive .='&search='.$search;
// here's my alternatives of array_replace_recursive(). 
// starts here...
    $options = array();
    $options1= array(
                'base_url' => site_url( $this->module ).'?'.$keepLive,
                'total_rows' => 0 ,
                'per_page' => $this->per_page
    );
    foreach($options1 as $key => $val) {
        $options[$key] = $val;
    }
    $toptions = $options;
    //end's here...
    /*          
            // so here's the array_replace_recursive() that i need to replace for alternatives.
            $toptions = array_replace_recursive( array(
                'base_url' => site_url( $this->module ).'?'.$keepLive,
                'total_rows' => 0 ,
                'per_page' => $this->per_page,
            ), $options ); 
            $this->pagination->initialize( $toptions );
     */
            $this->pagination->initialize( $toptions );
            return $this->pagination->create_links();
        } 
?>

如果您正在寻找替代array_replace_recursive()的函数,您应该检查一下:http://php.net/manual/en/function.array-replace-recursive.php

检查最上面的答案:

很高兴这个函数终于找到了它的PHP核心!如果你想在5.3.0之前的旧PHP版本中也使用它,你可以这样定义它:

<?php
if (!function_exists('array_replace_recursive'))
{
  function array_replace_recursive($array, $array1)
  {
    function recurse($array, $array1)
    {
      foreach ($array1 as $key => $value)
      {
        // create new key in $array, if it is empty or not an array
        if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key])))
        {
          $array[$key] = array();
        }
        // overwrite the value in the base array
        if (is_array($value))
        {
          $value = recurse($array[$key], $value);
        }
        $array[$key] = $value;
      }
      return $array;
    }
    // handle the arguments, merge one by one
    $args = func_get_args();
    $array = $args[0];
    if (!is_array($array))
    {
      return $array;
    }
    for ($i = 1; $i < count($args); $i++)
    {
      if (is_array($args[$i]))
      {
        $array = recurse($array, $args[$i]);
      }
    }
    return $array;
  }
}
?>

我在以前的项目中调用了这个函数array_marge_recursive_overwrite(),但array_replace_recursive()听起来更好。

如果你以前实现了这样一个兼容的函数,但不想重构所有代码,可以使用以下代码片段进行更新为了使用PHP 5.3.0的本地(希望更快)实现,如果可用。只需从以下几行开始您的功能:

<?php
  // as of PHP 5.3.0 array_replace_recursive() does the work for us
  if (function_exists('array_replace_recursive'))
  {
    return call_user_func_array('array_replace_recursive', func_get_args());
  }
?>
<?php
  // as of PHP 5.3.0 array_replace_recursive() does the work for us
  if (function_exists('array_replace_recursive'))
  {
    return call_user_func_array('array_replace_recursive', func_get_args());
  }
?>