【发布时间】:2021-07-28 21:03:18
【问题描述】:
我正在尝试对多维数组进行排序。我尝试过:
$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) "" } } }
有什么想法我在这里可能做错了吗?
【问题讨论】:
-
usort的回调应该在 $a 大于 $b 时返回 1,如果 $a 小于 $b 则返回 -1,或者如果 $a == $b 则返回 0。return $a['suggested_play_order'] > $b['suggested_play_order'];将返回 1 或 0,因此它永远不会认为 $a 应该放在 $b 之前。更好的返回语句是return $a['suggested_play_order'] > $b['suggested_play_order'] ? 1 : -1;,但您也应该检查它们是否相等。 -
@David 感谢您的快速回复,但 var_dump 仍然返回相同的结果。另外,为了澄清一下,在这种情况下顺序永远不会相同,但感谢您对未来知识的指导。
-
通过输出我认为您缺少索引,
$a["match"]['suggested_play_order'] > $b["match"]['suggested_play_order']; -
您的解决方案有效!感谢您指出这一点!
标签: php multidimensional-array