根据其值对PHP数组排序

时间:2019-09-11 14:58:53

标签: php

我做了

$result = array_count_values(Visitor::all()->pluck('device')->toArray());
$result = asort($result,SORT_NATURAL);
return $result;

我不断得到

  

Response内容必须是实现__toString()的字符串或对象,并给出“ boolean”。

enter image description here

这是一个数组

array_count_values(Visitor::all()->pluck('device')->toArray())

返回

{
    iPhone: 202,
    Windows NT 6.1: 2428,
    Windows NT 10.0: 2588,
    Macintosh: 1397,
    iPad: 12,
    Windows NT 6.2: 50,
    Windows NT 6.3: 90,
    X11: 442,
    compatible: 1813,
    Windows NT 5.1: 97,
    Linux: 227,
    Windows: 86,
    TweetmemeBot/4.0: 8,
    ) { :: 14,
    Windows NT 6.0: 7,
    User-Agent,Mozilla/5.0 : 1,
    KHTML, like Gecko: 6,
    Unknown: 11,
    Android: 1,
    Android 7.1.1: 1,
    Android 7.1.2: 2,
    Windows NT x.y: 2,
    Windows NT 6.1) AppleWebKit/537.36 : 7,
    Windows NT 5.0: 1,
    Windows NT 8.0: 1,
    web crawler :: robots.txt exclude elefent: 1,
    Windows NT: 1,
    Linux 4.4.0-116-generic: 1
}

我想基于values对它们进行排序。

请帮助

3 个答案:

答案 0 :(得分:1)

asort()返回一个布尔值,指示操作是否成功。您要做的是:

$result = array_count_values(Visitor::all()->pluck('device')->toArray());
asort($result,SORT_NATURAL);

return $result;

有关更多信息,请参见documentation

答案 1 :(得分:1)

asort返回一个布尔值,指示是否对排序进行了管理。它按引用对数组进行排序。

将代码更改为:

$result = array_count_values(Visitor::all()->pluck('device')->toArray());
asort($result,SORT_NATURAL);
//$result is now sorted.
return $result;

答案 2 :(得分:1)

asort()不返回排序后的数组,而是一个布尔值,表示是否已完成(true)(false

$result = array_count_values(Visitor::all()->pluck('device')->toArray());
asort($result, SORT_NATURAL);
return $result;
相关问题