Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel因工作不正常而多次受伤_Php_Laravel_Laravel 5_Laravel Collection - Fatal编程技术网

Php Laravel因工作不正常而多次受伤

Php Laravel因工作不正常而多次受伤,php,laravel,laravel-5,laravel-collection,Php,Laravel,Laravel 5,Laravel Collection,我对laravel sortBy(laravel 5.4)有一些问题。。根据我在许多网站上读到的内容,它说,拉雷维尔做多件坏事是通过使用相反的顺序。。所以我试着这么做。。但是仍然不能正常工作 就是这样。。 我收集了一些东西 [{ 'product_id' => 468, 'name' => 'abc', 'int_premi' => 10000 'score' => 1000 'rates' => 0, 'views' =>

我对laravel sortBy(laravel 5.4)有一些问题。。根据我在许多网站上读到的内容,它说,拉雷维尔做多件坏事是通过使用相反的顺序。。所以我试着这么做。。但是仍然不能正常工作

就是这样。。 我收集了一些东西

[{
   'product_id' => 468,
   'name' => 'abc',
   'int_premi' => 10000
   'score' => 1000
   'rates' => 0,
   'views' => 0,
   'promo' => null
},{
   'product_id' => 472,
   'name' => 'bcd',
   'int_premi' => 10000
   'score' => 1000
   'rates' => 0,
   'views' => 0,
   'promo' => 'Some text here'
},{
   'product_id' => 458,
   'name' => 'def',
   'int_premi' => 10000
   'score' => 1000
   'rates' => 0,
   'views' => 0,
   'promo' => 'ABC'
}]
我的目标是按照这个顺序对这些对象进行排序

得分(asc)>内部薪酬(asc)>费率(desc)>促销(as) 布尔值(描述)>视图(描述)>产品标识(描述)

所以我写了这个代码

$collection->sortByDesc('product_id')->sortByDesc('views')->sortByDesc(function($arr,$k){
                            return !empty($arr->promo);
    })->sortByDesc('rates')->sortBy('int_premi')->sortBy('score')->values()->all()
我在找这个订单的结果

BCD>DEF>ABC

相反,不要遵循这个顺序

那么,有没有人也和我面临同样的问题?也许有人能帮我解决这个问题


非常感谢

此代码可以根据您的需要正常工作。但如果您想修改某些内容,您可以发表评论:

$collection = ([[
   'product_id' => 468,
   'name' => 'abc',
   'int_premi' => 10000,
   'score' => 1000,
   'rates' => 0,
   'views' => 0,
   'promo' => null
],[
   'product_id' => 472,
   'name' => 'bcd',
   'int_premi' => 10000,
   'score' => 1000,
   'rates' => 0,
   'views' => 0,
   'promo' => 'Some text here'
],[
   'product_id' => 458,
   'name' => 'def',
   'int_premi' => 10000,
   'score' => 1000,
   'rates' => 0,
   'views' => 0,
   'promo' => 'ABC'
]]);

$collection = collect($collection)->sortByDesc('product_id')->sortByDesc('views')->sortByDesc('promo')->sortByDesc('rates')->sortBy('int_premi')->sortBy('score')->values()->all();
return $collection;

在做了一些研究之后。。我发现那个正在工作。。 如果你面对的是同一件事。。这篇文章可能会有帮助

因此,我的最后一个建议与本文的建议完全相同,即创建一个宏。。像这样

if (!Collection::hasMacro('sortByMulti')) {
    /**
     * An extension of the {@see Collection::sortBy()} method that allows for sorting against as many different
     * keys. Uses a combination of {@see Collection::sortBy()} and {@see Collection::groupBy()} to achieve this.
     *
     * @param array $keys An associative array that uses the key to sort by (which accepts dot separated values,
     *                    as {@see Collection::sortBy()} would) and the value is the order (either ASC or DESC)
     */
    Collection::macro('sortByMulti', function (array $keys) {
        $currentIndex = 0;
        $keys = array_map(function ($key, $sort) {
            return ['key' => $key, 'sort' => $sort];
        }, array_keys($keys), $keys);

        $sortBy = function (Collection $collection) use (&$currentIndex, $keys, &$sortBy) {
            if ($currentIndex >= count($keys)) {
                return $collection;
            }

            $key = $keys[$currentIndex]['key'];
            $sort = $keys[$currentIndex]['sort'];
            $sortFunc = $sort === 'DESC' ? 'sortByDesc' : 'sortBy';
            $currentIndex++;
            return $collection->$sortFunc($key)->groupBy($key)->map($sortBy)->ungroup();
        };

        return $sortBy($this);
    });
}
$collection->sortByMulti([
    'prop_one'       => 'ASC',
    'prop_two'        => 'ASC',
    etc....
]);
然后你可以像这样在你的收藏中使用它

if (!Collection::hasMacro('sortByMulti')) {
    /**
     * An extension of the {@see Collection::sortBy()} method that allows for sorting against as many different
     * keys. Uses a combination of {@see Collection::sortBy()} and {@see Collection::groupBy()} to achieve this.
     *
     * @param array $keys An associative array that uses the key to sort by (which accepts dot separated values,
     *                    as {@see Collection::sortBy()} would) and the value is the order (either ASC or DESC)
     */
    Collection::macro('sortByMulti', function (array $keys) {
        $currentIndex = 0;
        $keys = array_map(function ($key, $sort) {
            return ['key' => $key, 'sort' => $sort];
        }, array_keys($keys), $keys);

        $sortBy = function (Collection $collection) use (&$currentIndex, $keys, &$sortBy) {
            if ($currentIndex >= count($keys)) {
                return $collection;
            }

            $key = $keys[$currentIndex]['key'];
            $sort = $keys[$currentIndex]['sort'];
            $sortFunc = $sort === 'DESC' ? 'sortByDesc' : 'sortBy';
            $currentIndex++;
            return $collection->$sortFunc($key)->groupBy($key)->map($sortBy)->ungroup();
        };

        return $sortBy($this);
    });
}
$collection->sortByMulti([
    'prop_one'       => 'ASC',
    'prop_two'        => 'ASC',
    etc....
]);
道具一和道具二是你的收藏。。
希望此帮助

您阅读了吗?刚刚阅读了,这是来自laravel的错误?原因是底层PHP函数。从第二条到最后一条评论中的解决方法对您有帮助吗?正如@JonasStaudenmeir所说,这是底层PHP函数的一部分,不幸的是,在处理不同的PHP版本时会导致严重的问题。曾经有人试图在laravel 5.5中解决这个问题,但你现在是在5.4中。检查您正在运行的php版本,然后检查asort和arsort函数在服务器上的工作情况,以了解您想要的行为。最后,我的建议是,编写单元测试来检查它是否在您的应用程序中工作。然后,在更改环境时,您将知道是否需要修改某些内容。我知道这感觉甚至不接近最佳状态:(谢谢你们回答我的问题..我找到了解决方案..我也把它放在了这个问题的答案上..所以其他面对这个问题的人可能能够解决他们的问题..谢谢..我尝试了这个解决方案,并向我展示了ungroup方法不存在,is也是另一个宏,你可以在这里找到完整的代码: