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

时间:2016-05-26 06:39:00

标签: php arrays slice usort

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 [...]

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

2 个答案:

答案 0 :(得分: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;}}

答案 1 :(得分:0)

我来晚了,但是$ this-> $ data应该是$ this-> data。删除“数据”前面的“ $”。