PHP警告:usort()期望参数1为数组,在&array_slice()中给出null,期望参数1为数组,null为

问题描述 投票:0回答:2
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;}

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

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

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

任何人都可以帮我解决他的问题吗?

php arrays slice usort
2个回答
0
投票

我认为$ 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;}}

0
投票

我迟到了这个派对,但$ this - > $ data应该是$ this-> data。删除“数据”前面的“$”。

© www.soinside.com 2019 - 2024. All rights reserved.