首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP警告: usort()要求参数1为数组,& array_slice()中给出的null要求参数1为数组,在

PHP警告: usort()要求参数1为数组,& array_slice()中给出的null要求参数1为数组,在
EN

Stack Overflow用户
提问于 2016-05-26 14:39:00
回答 2查看 6.3K关注 0票数 0
代码语言:javascript
运行
复制
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;}

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

代码语言:javascript
运行
复制
Warning: usort() expects parameter 1 to be array, null given in [...]

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

有人能帮我解决他的问题吗?

EN

回答 2

Stack Overflow用户

发布于 2016-05-26 14:44:41

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

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2019-03-04 18:42:13

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37453304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档