Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel short collection by keys based on array

$collection = collect([
    ['name' => 'A', 'status' => 0],
    ['name' => 'B', 'status' => 3],
    ['name' => 'C', 'status' => 0],
    ['name' => 'D', 'status' => 1],
    ['name' => 'E', 'status' => 2],
    ['name' => 'F', 'status' => 1],
    ['name' => 'G', 'status' => 3],
    ['name' => 'H', 'status' => 1],
    ['name' => 'I', 'status' => 2],
    ['name' => 'J', 'status' => 3],
]);

// define status priority here
$statusPriorities = [2, 1, 3, 0];

$collection->sortBy(function($order) use($statusPriorities){
   return array_search($order['status'], $statusPriorities);
})->values()->all();
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #laravel #short #collection #keys #based #array
ADD COMMENT
Topic
Name
8+7 =