I have a list of arrays with values inside each array. I want to sort my list based on one of these values so that unique arrays are displayed first, then duplicate arrays.
I don’t want any member to be removed from the array, I just want my array to be sorted
this is the list that I have
array:6 [▼
0 => array:8 [▼
"id" => 35
"answer" => null
"type" => 3
"user_id" => 9
"question_id" => 36
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
1 => array:8 [▼
"id" => 36
"answer" => null
"type" => 3
"user_id" => 9
"question_id" => 36
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
2 => array:8 [▼
"id" => 37
"answer" => "Gello"
"type" => 0
"user_id" => 9
"question_id" => 37
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
3 => array:8 [▼
"id" => 38
"answer" => "Uho"
"type" => 0
"user_id" => 9
"question_id" => 37
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
4 => array:8 [▼
"id" => 39
"answer" => "Who"
"type" => 1
"user_id" => 9
"question_id" => 38
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
5 => array:8 [▼
"id" => 40
"answer" => "Window"
"type" => 1
"user_id" => 9
"question_id" => 38
"answer_id" => 1
"created_at" => "2023-04-07T09:03:40.000000Z"
"updated_at" => "2023-04-07T09:03:40.000000Z"
]
]
and I want to sort by question_id
. I want my list to eventually become this list
array:6 [▼
0 => array:8 [▼
"id" => 35
"answer" => null
"type" => 3
"user_id" => 9
"question_id" => 36
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
2 => array:8 [▼
"id" => 37
"answer" => "Hello"
"type" => 0
"user_id" => 9
"question_id" => 37
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
4 => array:8 [▼
"id" => 39
"answer" => "Who"
"type" => 1
"user_id" => 9
"question_id" => 38
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
1 => array:8 [▼
"id" => 36
"answer" => null
"type" => 3
"user_id" => 9
"question_id" => 36
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
3 => array:8 [▼
"id" => 38
"answer" => "Uho"
"type" => 0
"user_id" => 9
"question_id" => 37
"answer_id" => 1
"created_at" => "2023-04-07T09:03:39.000000Z"
"updated_at" => "2023-04-07T09:03:39.000000Z"
]
5 => array:8 [▼
"id" => 40
"answer" => "Window"
"type" => 1
"user_id" => 9
"question_id" => 38
"answer_id" => 1
"created_at" => "2023-04-07T09:03:40.000000Z"
"updated_at" => "2023-04-07T09:03:40.000000Z"
]
]
3
Answers
Well, it looks like returned data from DB, it depends on that what you use:
DB class -> https://laravel.com/docs/10.x/queries#ordering
DB::table('table_name')->orderBy('question_id')->get()->toArray();
Eloquent -> https://laravel.com/docs/10.x/eloquent#retrieving-models
Model::orderBy('question_id)->get()->toArray();
as you mention you are having an array this can be a proper solution for it
but it is a Larvel collection then you can sort method of it with same call back method.
You would actually need to use a map/set to store rows per each question ID. Then use
array_column
andarray_merge
to make them appear group by group.Note: Since yours is a Laravel collections, you can simply use
->toArray()
to convert it to a PHP array and use the code below!Snippet:
Online Demo