php - 对多维数组进行排序未正确排序
问题描述
我正在尝试对多维数组进行排序。我尝试过:
$matches = array_column($matches,null,"suggested_play_order");
usort($matches, 'sortByPlayOrder');
function sortByPlayOrder($a, $b) {
return $a['suggested_play_order'] > $b['suggested_play_order'];
}
和
array_multisort(array_column($matches,null,"suggested_play_order"),SORT_NUMERIC, $matches);
和
array_multisort(array_column($matches,null,"suggested_play_order"),SORT_ASC, $matches);
这些都不会将以下内容返回到按建议播放顺序升序排序的正确顺序。
var_dump($matches); 返回:
array(15) { [0]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771351) ["tournament_id"]=> int(10093916) ["state"]=> string(4) "open" ["player1_id"]=> int(151601125) ["player2_id"]=> int(151601126) ["player1_prereq_match_id"]=> NULL ["player2_prereq_match_id"]=> NULL ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> string(29) "2021-07-28T12:55:28.144-04:00" ["created_at"]=> string(29) "2021-07-28T12:55:27.996-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.144-04:00" ["identifier"]=> string(1) "A" ["has_attachment"]=> bool(false) ["round"]=> int(1) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(1) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(0) "" ["scores_csv"]=> string(0) "" } } [1]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771352) ["tournament_id"]=> int(10093916) ["state"]=> string(4) "open" ["player1_id"]=> int(151601129) ["player2_id"]=> int(151601132) ["player1_prereq_match_id"]=> NULL ["player2_prereq_match_id"]=> NULL ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> string(29) "2021-07-28T12:55:28.191-04:00" ["created_at"]=> string(29) "2021-07-28T12:55:28.002-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.191-04:00" ["identifier"]=> string(1) "B" ["has_attachment"]=> bool(false) ["round"]=> int(1) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(2) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(0) "" ["scores_csv"]=> string(0) "" } } [2]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771353) ["tournament_id"]=> int(10093916) ["state"]=> string(4) "open" ["player1_id"]=> int(151601127) ["player2_id"]=> int(151601130) ["player1_prereq_match_id"]=> NULL ["player2_prereq_match_id"]=> NULL ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> string(29) "2021-07-28T12:55:28.203-04:00" ["created_at"]=> string(29) "2021-07-28T12:55:28.007-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.203-04:00" ["identifier"]=> string(1) "C" ["has_attachment"]=> bool(false) ["round"]=> int(1) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(3) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(0) "" ["scores_csv"]=> string(0) "" } } [3]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771354) ["tournament_id"]=> int(10093916) ["state"]=> string(4) "open" ["player1_id"]=> int(151601128) ["player2_id"]=> int(151601131) ["player1_prereq_match_id"]=> NULL ["player2_prereq_match_id"]=> NULL ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> string(29) "2021-07-28T12:55:28.216-04:00" ["created_at"]=> string(29) "2021-07-28T12:55:28.011-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.216-04:00" ["identifier"]=> string(1) "D" ["has_attachment"]=> bool(false) ["round"]=> int(1) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(4) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(0) "" ["scores_csv"]=> string(0) "" } } [4]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771355) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771351) ["player2_prereq_match_id"]=> int(245771352) ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.015-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.015-04:00" ["identifier"]=> string(1) "E" ["has_attachment"]=> bool(false) ["round"]=> int(2) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(7) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771351,245771352" ["scores_csv"]=> string(0) "" } } [5]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771356) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771353) ["player2_prereq_match_id"]=> int(245771354) ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.021-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.021-04:00" ["identifier"]=> string(1) "F" ["has_attachment"]=> bool(false) ["round"]=> int(2) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(8) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771353,245771354" ["scores_csv"]=> string(0) "" } } [6]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771357) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771355) ["player2_prereq_match_id"]=> int(245771356) ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.025-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.025-04:00" ["identifier"]=> string(1) "G" ["has_attachment"]=> bool(false) ["round"]=> int(3) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(12) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771355,245771356" ["scores_csv"]=> string(0) "" } } [7]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771358) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771351) ["player2_prereq_match_id"]=> int(245771352) ["player1_is_prereq_match_loser"]=> bool(true) ["player2_is_prereq_match_loser"]=> bool(true) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.030-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.030-04:00" ["identifier"]=> string(1) "J" ["has_attachment"]=> bool(false) ["round"]=> int(-1) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(5) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771351,245771352" ["scores_csv"]=> string(0) "" } } [8]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771359) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771353) ["player2_prereq_match_id"]=> int(245771354) ["player1_is_prereq_match_loser"]=> bool(true) ["player2_is_prereq_match_loser"]=> bool(true) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.035-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.035-04:00" ["identifier"]=> string(1) "K" ["has_attachment"]=> bool(false) ["round"]=> int(-1) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(6) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771353,245771354" ["scores_csv"]=> string(0) "" } } [9]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771360) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771356) ["player2_prereq_match_id"]=> int(245771358) ["player1_is_prereq_match_loser"]=> bool(true) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.039-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.039-04:00" ["identifier"]=> string(1) "L" ["has_attachment"]=> bool(false) ["round"]=> int(-2) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(10) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771356,245771358" ["scores_csv"]=> string(0) "" } } [10]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771361) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771355) ["player2_prereq_match_id"]=> int(245771359) ["player1_is_prereq_match_loser"]=> bool(true) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.045-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.045-04:00" ["identifier"]=> string(1) "M" ["has_attachment"]=> bool(false) ["round"]=> int(-2) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(9) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771355,245771359" ["scores_csv"]=> string(0) "" } } [11]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771362) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771360) ["player2_prereq_match_id"]=> int(245771361) ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.050-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.050-04:00" ["identifier"]=> string(1) "N" ["has_attachment"]=> bool(false) ["round"]=> int(-3) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(11) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771360,245771361" ["scores_csv"]=> string(0) "" } } [12]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771363) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771357) ["player2_prereq_match_id"]=> int(245771362) ["player1_is_prereq_match_loser"]=> bool(true) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.054-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.054-04:00" ["identifier"]=> string(1) "O" ["has_attachment"]=> bool(false) ["round"]=> int(-4) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(13) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771357,245771362" ["scores_csv"]=> string(0) "" } } [13]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771364) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771357) ["player2_prereq_match_id"]=> int(245771363) ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(false) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.058-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.058-04:00" ["identifier"]=> string(1) "H" ["has_attachment"]=> bool(false) ["round"]=> int(4) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(14) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(19) "245771357,245771363" ["scores_csv"]=> string(0) "" } } [14]=> array(1) { ["match"]=> array(34) { ["id"]=> int(245771365) ["tournament_id"]=> int(10093916) ["state"]=> string(7) "pending" ["player1_id"]=> NULL ["player2_id"]=> NULL ["player1_prereq_match_id"]=> int(245771364) ["player2_prereq_match_id"]=> int(245771364) ["player1_is_prereq_match_loser"]=> bool(false) ["player2_is_prereq_match_loser"]=> bool(true) ["winner_id"]=> NULL ["loser_id"]=> NULL ["started_at"]=> NULL ["created_at"]=> string(29) "2021-07-28T12:55:28.063-04:00" ["updated_at"]=> string(29) "2021-07-28T12:55:28.063-04:00" ["identifier"]=> string(1) "I" ["has_attachment"]=> bool(false) ["round"]=> int(4) ["player1_votes"]=> NULL ["player2_votes"]=> NULL ["group_id"]=> NULL ["attachment_count"]=> NULL ["scheduled_time"]=> NULL ["location"]=> NULL ["underway_at"]=> NULL ["optional"]=> bool(false) ["rushb_id"]=> NULL ["completed_at"]=> NULL ["suggested_play_order"]=> int(15) ["forfeited"]=> NULL ["open_graph_image_file_name"]=> NULL ["open_graph_image_content_type"]=> NULL ["open_graph_image_file_size"]=> NULL ["prerequisite_match_ids_csv"]=> string(9) "245771364" ["scores_csv"]=> string(0) "" } } }
有什么想法我可能在这里做错了吗?
解决方案
每个数组都有一个match
索引,其中包含一个数组,该数组包含suggested_play_order
. 您可以使用array_column
两次提取match
索引下的所有数组,然后suggested_play_order
从那里提取:
array_multisort(array_column(array_column($matches, "match"), "suggested_play_order"), $matches);
此外,作为第二个参数和一个索引作为第三个参数传递null
返回由第三个参数索引的整个数组。不是你想要的。
推荐阅读
- c# - 简单的 ASP.NET Core 路由问题
- java - java和ntdll.dll的关系
- npm - 如何解决 gulp watch 错误
- php - Wordpress Yoast 插件打开图表以从作者页面获取头像
- c# - 带有 Guid 的显式结构布局
- javascript - 如何在 node.js 中访问父对象的名称?
- sql - 什么样的 SQL 查询会得到想要的结果?
- javascript - window.getSelection().getRange(0) 在文本被包裹时不起作用
- netlogo - Netlogo:平均集群大小
- sas - Proc Transpose 每个组有多个 ID 值