首页 \ 问答 \ usort()警告虽然提交数组(usort() warning although submitting array)

usort()警告虽然提交数组(usort() warning although submitting array)

为什么不对数组进行usort()排序?

if ( is_array( $tables ) ) {
    usort( $tables, 'sort' );
} else {
    echo "no array";
}

我总是得到这个警告:

sort()期望参数1是数组,给定字符串

所以php认为它是一个数组,但usort()不是

继承人的排序功能:

function sort( $a, $b ) {
    return strlen( $b ) - strlen( $a );
}

Why doesn't usort() sort the array?

if ( is_array( $tables ) ) {
    usort( $tables, 'sort' );
} else {
    echo "no array";
}

I always get this warning:

sort() expects parameter 1 to be array, string given

so php thinks its an array but usort() not

heres the sort function:

function sort( $a, $b ) {
    return strlen( $b ) - strlen( $a );
}

原文:https://stackoverflow.com/questions/10542724
更新时间:2019-06-23 01:10

最满意答案

注意错误说sort() expects ,而不是usort() expects 。 这是因为PHP将回调解释为内置的sort()方法(它希望第一个参数是一个数组),而不是sort()方法。

尝试将您的方法重命名为其他内容,例如my_sort

function my_sort( $a, $b ) {
    return strlen( $b ) - strlen( $a );
}

if ( is_array( $tables ) ) {
    usort( $tables, 'my_sort' );
} else {
    echo "no array";
}

Note the error says sort() expects, not usort() expects. That's because PHP is interpreting the callback to usort as the built-in sort() method (which expects the 1st parameter to be an array), not your sort() method.

Try renaming your method to something else, like my_sort.

function my_sort( $a, $b ) {
    return strlen( $b ) - strlen( $a );
}

if ( is_array( $tables ) ) {
    usort( $tables, 'my_sort' );
} else {
    echo "no array";
}
2012-05-10

相关问答

更多

从codeigniter中的视图提交大型数组(Submitting a large array from a view in codeigniter)

您的控制器需要更正: $this->load->library('session'); $list['questions'] = $this->assessment_model->get_list_questions($data['list']); $this->load->view('quiz', $list); 在视图中: <?php $this->session->set_flashdata('Questions', $questions); $this->load->he ...

在PHP数组定义中显示重复的键警告(Show duplicated key warning in PHP array definition)

你唯一的希望(除了自己计算)是你的编辑很聪明,可以突出显示错字。 此屏幕截图来自PHPStorm: Your only hope (apart from count-ing yourself) is that your editor is smart enough to highlight the typo. This screenshot is from PHPStorm:

although与through的用法与区别

Although 和 though 都是表示让步---即使、虽然的意思,基本可以互换,算是连词范畴。唯一区别就是though可不放在句首--即可以当副词使用,although则必须放在句首。 through是介词 用于动词之后 多表示 通过(手段 方法 空间) 的意思。

提交到数据库(Submitting to database)

你的逻辑错了。 试试这段代码: <?php // src/AppBundle/Controller/ProductController.php namespace Upload\FileBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Compone ...

array_shift()不返回数组[关闭](array_shift() does not return an array [closed])

array_shift不返回数组,它返回传入的数组的第一个值。传入的数组被修改。 array_shift doesn't return an array, it returns the first value of the array passed in. The array passed in is modified.

在wordpress中提交表单时出错(error with submitting a form in wordpress)

在WordPress StackExchange站点上查看此问题 。 总之,WordPress被名为name的字段搞糊涂了。 尝试将其重命名为(比如) contact_name 。 在这里加载更多细节(也是wpse)。 See this question on the WordPress StackExchange site. In summary, WordPress gets confused by a field called name. Try renaming it to (say) c ...

在编译C90代码时,在gcc编译器中获取警告“ISO C90禁止可变大小数组”(Getting the warning “ ISO C90 forbids variable-size array” in gcc compiler while compiling C90 code)

您收到该警告,因为C90不支持可变长度数组 。 您必须使用-std = c99或std = gnu99命令行标志将gcc切换到C99模式(支持vla),或者重写代码以动态分配内存或使用固定大小的数组。 警告只是告诉你在这种情况下你不符合C90,但它是安全的。 忽略警告应该不是一个选择。 You get that warning because C90 does not support variable length arrays. You'll either have to switch gcc ...

提交表格(Submitting Forms)

onClick = document.form1.submit(); onClick = document.form1.submit();

相关文章

更多

最新问答

更多
  • Android宽度:100%修复(网站接管问题)(Android width:100% fix (website takeover issue))
  • C ++函数/方法设计的良好实践(Good practice in C++ function/method design)
  • 计算其他表中不存在的所有记录 - SQL查询(Count all records that does not exist to other table - SQL Query)
  • 为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)
  • asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)
  • 我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)
  • ng-class搞乱了类的顺序(ng-class messing with the order of classes)
  • oracle 12g无效数字错误(oracle 12g invalid number error)
  • 更改ng-src值onclick(Change ng-src value onclick)
  • 如何在android中自动添加自定义依赖项以创建新项目?(How to add custom dependencies automatically in android for ever a new project is created?)
  • datetime函数在PHP中(datetime function in php)
  • 在javascript中获取会话数组的值(in javascript get the value of a session array)
  • 如何在UTF8中编译LaTeX?(How can I compile LaTeX in UTF8? [closed])
  • Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)
  • Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)
  • JavaFX的。(JavaFX. Adding items to the list in different threads is not working)
  • 从GDATA日历资源迁移到Google Calendar Resource api(Migrate from GDATA calendar resource to Google Calendar Resource api)
  • SSRS 2008 - 以零情景处理分割(SSRS 2008 - Dealing with division by zero scenarios)
  • 我如何以编程方式添加一个listView列标题的点击事件(How can I add a listView column header a click event programmatically)
  • Wxpython:无法检索有关列表控件项XXX的信息(Wxpython: Couldn't retrieve information about list control item XXX)
  • 使用Tortoise SVN在SVN存储库中移动目录(Move Directory across SVN repository using Tortoise SVN)
  • 天蓝色服务结构集群中的web api无状态服务是否在一段时间不活动后进入休眠状态?(Do web api stateless services in azure service fabric cluster go to sleep after a period of inactivity?)
  • 我可以设置intelliJ来突出显示PHP编码风格吗?(Can I set intelliJ to highlight php coding style?)
  • 用javafx创建一个Truetype字体文件(Creating a Truetype Font file with javafx)
  • Spring ftp配置错误(Spring ftp configuration is wrong)
  • 使用gsub去除多个字符(Using gsub to strip multiple characters)
  • 续订推送证书并保持当前的App Store App正常工作(Renew Push certificate and keep current App Store App working)
  • js:ES5和ES6之间关于'this'关键字用法的一个令人困惑的观点(js: one confusing point about 'this' keyword usage between ES5 and ES6)
  • window.onload vs $(document).ready()(window.onload vs $(document).ready())
  • 在Swift中,如何声明一个符合一个或多个协议的特定类型的变量?(In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?)