此问题已在此处有答案:
Sort a flat array in recurring ascending sequences(5个答案)
2天前关闭。
我有一个带值的数组列表 在每个数组中。我想根据这些值中的一个对列表进行排序 这样唯一的数组会先显示,然后是重复的数组。我不想从数组中删除任何成员,我只想对数组进行排序
这是我的名单
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"
]
]
我想按question_id
排序。我希望我的列表最终变成这个列表
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条答案
按热度按时间p1tboqfb1#
实际上,您需要使用map/set来存储每个问题ID的行,然后使用
array_column
和array_merge
使它们按组显示。**注意:**由于您的是Laravel集合,您可以简单地使用
->toArray()
将其转换为PHP数组,并使用以下代码!片段:
Online Demo
vs3odd8k2#
好吧,它看起来像是从DB返回的数据,这取决于你使用的是什么:
DB::table('table_name')->orderBy('question_id')->get()->toArray();
Model::orderBy('question_id)->get()->toArray();
hgqdbh6s3#
正如你提到的,你有一个数组,这可能是一个合适的解决方案
但是它是一个Larvel集合,那么你可以用相同的回调方法对它进行排序。