2

Suppose I have a collection like this named 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"
    ]
]
}

On the other hand there is an array of IDs like this :

[11,10,1]

Now I want to sort element of that collection based on their cat_id index as sorted in an array. means I want result array to be like this :

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"
    ] 
]
}

I used below codes but does not work properly:

$grids_arr  = [11,10,1];

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

Update :
This is my completed Code I'm using:

        $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);
            });
        }
Ahmad Badpey
  • 6,348
  • 16
  • 93
  • 159

2 Answers2

2

please check the code below.

$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);

Hope that it helps you.

Jesus Erwin Suarez
  • 1,571
  • 16
  • 17
2

You probably have not responded because of differences between config('settings.data_grids') And $categories. So edit your code as follows

Please add these three lines to your code:

$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

for example:

        $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();
        }
Ali Sharifi Neyestani
  • 4,162
  • 1
  • 14
  • 22