【问题标题】:PHP Warning: usort() expects parameter 1 to be array, null given in & array_slice() expects parameter 1 to be array, null given inPHP 警告:usort() 期望参数 1 为数组,空值在 & array_slice() 期望参数 1 为数组,空值在
【发布时间】:2016-09-24 00:17:38
【问题描述】:
function prepare_items() {
  $columns  = $this->get_columns();
  $hidden   = array();
  $sortable = $this->get_sortable_columns();
  $this->_column_headers = array( $columns, $hidden, $sortable );
  usort( $this->$data, array( &$this, 'usort_reorder' ) );
  $per_page = 5;
  $current_page = $this->get_pagenum();
  $total_items = count( $this->data );
  $this->found_data = array_slice( $this->data,( ( $current_page-1 )* $per_page ), $per_page );
  $this->set_pagination_args( array(
    'total_items' => $total_items,
    'per_page'    => $per_page
  ) );
  $this->items = $this->found_data;}

我从上面的代码中得到了 thia 错误。

Warning: usort() expects parameter 1 to be array, null given in [...]

Warning: array_slice() expects parameter 1 to be array, null given in [...]

谁能帮我解决他的问题?

【问题讨论】:

  • $this->$data 是一个数组吗?
  • 我认为 $this->$data 返回空

标签: php arrays slice usort


【解决方案1】:

我认为 $this->$data 返回空。确保 $this->$data 返回数组并检查值 (if(!empty($this->$data)) 并继续执行函数

function prepare_items() {
  $columns  = $this->get_columns();
  $hidden   = array();
  $sortable = $this->get_sortable_columns();
  $this->_column_headers = array( $columns, $hidden, $sortable );
if(!empty($this->$data) {
  usort( $this->$data, array( &$this, 'usort_reorder' ) );
  $per_page = 5;
  $current_page = $this->get_pagenum();
  $total_items = count( $this->data );
  $this->found_data = array_slice( $this->data,( ( $current_page-1 )* $per_page ), $per_page );
  $this->set_pagination_args( array(
    'total_items' => $total_items,
    'per_page'    => $per_page
  ) );
  $this->items = $this->found_data;}}

【讨论】:

    【解决方案2】:

    我来晚了,但 $this->$data 应该是 $this->data。去掉“data”前面的“$”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-05
      • 2020-06-17
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多