Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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中的ID数组对集合进行排序_Php_Laravel - Fatal编程技术网

Php 根据Laravel中的ID数组对集合进行排序

Php 根据Laravel中的ID数组对集合进行排序,php,laravel,Php,Laravel,假设我有一个这样的集合,名为categories: Collection {#447 #items: array:3 [ 0 => array:6 [ "pos" => "0" "col" => "1" "row" => "1" "size_x" => "2" "size_y" => "1" "cat_id" => "1" ] 1 => array:

假设我有一个这样的集合,名为
categories

Collection {#447
  #items: array:3 [
    0 => array:6 [
      "pos" => "0"
      "col" => "1"
      "row" => "1"
      "size_x" => "2"
      "size_y" => "1"
      "cat_id" => "1"
    ]
    1 => array:6 [
      "pos" => "0"
      "col" => "3"
      "row" => "1"
      "size_x" => "1"
      "size_y" => "1"
      "cat_id" => "11"
    ]
    2 => array:6 [
      "pos" => "0"
      "col" => "1"
      "row" => "2"
      "size_x" => "2"
      "size_y" => "1"
      "cat_id" => "10"
    ]
]
}
另一方面,有这样一个ID数组:

[11,10,1]
Collection {#447
  #items: array:3 [
    0 => array:6 [
      "pos" => "0"
      "col" => "3"
      "row" => "1"
      "size_x" => "1"
      "size_y" => "1"
      "cat_id" => "11"
    ]
    1 => array:6 [
      "pos" => "0"
      "col" => "1"
      "row" => "2"
      "size_x" => "2"
      "size_y" => "1"
      "cat_id" => "10"
    ]
    2 =>array:6 [
      "pos" => "0"
      "col" => "1"
      "row" => "1"
      "size_x" => "2"
      "size_y" => "1"
      "cat_id" => "1"
    ] 
]
}
现在我想根据数组中排序的
cat\u id
索引对该集合的元素进行排序。表示我希望结果数组如下所示:

[11,10,1]
Collection {#447
  #items: array:3 [
    0 => array:6 [
      "pos" => "0"
      "col" => "3"
      "row" => "1"
      "size_x" => "1"
      "size_y" => "1"
      "cat_id" => "11"
    ]
    1 => array:6 [
      "pos" => "0"
      "col" => "1"
      "row" => "2"
      "size_x" => "2"
      "size_y" => "1"
      "cat_id" => "10"
    ]
    2 =>array:6 [
      "pos" => "0"
      "col" => "1"
      "row" => "1"
      "size_x" => "2"
      "size_y" => "1"
      "cat_id" => "1"
    ] 
]
}
我使用了以下代码,但无法正常工作:

$grids_arr  = [11,10,1];

$categories = $categories->sortBy(function ($model) use ($grids_arr) {
    return array_search($model->cat_id, $grids_arr);
});
更新:
这是我正在使用的完整代码:

        $categories = Category::isTile()->get();

        $grids = collect(config('settings.data_grids'));// This is an array of IDs
        if ($grids->isNotEmpty()) {

            $grids_arr = $grids->pluck("cat_id")->toArray();
            $grids_arr = array_map('intval', $grids_arr); //convert string array elements to integer

            $categories = $categories->sortBy(function ($catg) use ($grids_arr) {
                return array_search($catg->cat_id, $grids_arr);
            });
        }

请检查下面的代码

$array = [
    0 => [
      "pos" => "0",
      "col" => "3",
      "row" => "1",
      "size_x" => "1",
      "size_y" => "1",
      "cat_id" => "11",
    ],
    1 => [
      "pos" => "0",
      "col" => "1",
      "row" => "2",
      "size_x" => "2",
      "size_y" => "1",
      "cat_id" => "10",

    ],
    2  => [

      "pos" => "0",
      "col" => "1",
      "row" => "1",
      "size_x" => "2",
      "size_y" => "1",
      "cat_id" => "1",
    ],
];

$array = array_reverse(array_sort($array, function ($value)
{
    return $value['cat_id'];
}));

dd($array);

希望它能对您有所帮助。

由于配置('settings.data_grids')和$categories之间的差异,您可能没有响应。因此,请按如下方式编辑代码

请在代码中添加以下三行:

$catIds = $categories->pluck('cat_id')->toArray(); //get all cat_id
$diff = array_diff($catIds, $grids_arr); // difference array from query
$grids_arr = array_merge($grids_arr , $diff); //merge difference with array
例如:

        $categories = Category::select('*')->get(); //for example
        $array = [
            [
                "pos" => "0",
                "col" => "1",
                "row" => "1",
                "size_x" => "2",
                "size_y" => "1",
                "cat_id" => 1,
            ],
            [
                "pos" => "0",
                "col" => "1",
                "row" => "2",
                "size_x" => "2",
                "size_y" => "1",
                "cat_id" => 10,
            ]
        ];
        $grids = collect($array);

        if ($grids->isNotEmpty()) {
            $grids_arr = $grids->pluck("cat_id")->toArray();
            $grids_arr = array_map('intval', $grids_arr); 

            //Please add these three lines to your code
            $catIds = $categories->pluck('cat_id')->toArray(); 
            $diff = array_diff($catIds, $grids_arr);
            $grids_arr = array_merge($grids_arr, $diff);
            //-----

            $sorted = $categories->sortBy(function ($model) use ($grids_arr) {
                return array_search($model->cat_id, $grids_arr);
            });


            return $sorted->values()->all();
        }

如果您可以通过
“cat_id”
为您的
$categories
集合编制索引,则您可以使用“我没有任何问题,请正确操作发布的排序”。你能进一步解释一下吗?我可以看出使用相同的cat_id有问题,因为array_search()将返回找到的第一个键,但我对上述代码的测试结果正常。
cat_id
这里是主键吗?@vivek_23,是的。我尝试了@jesus erwin suarez建议的方法,但没有得到正确的结果。我有一个集合不是arrayit真的有效。请将$grids\u arr更改为您的阵列是否需要匹配
$grids\u arr
阵列元素和集合的数量。