I’m attempting to sort a multidimensional array. I have attempted:
$matches = array_column($matches,null,"suggested_play_order"); usort($matches, 'sortByPlayOrder'); function sortByPlayOrder($a, $b) { return $a['suggested_play_order'] > $b['suggested_play_order']; }
and
array_multisort(array_column($matches,null,"suggested_play_order"),SORT_NUMERIC, $matches);
and
array_multisort(array_column($matches,null,"suggested_play_order"),SORT_ASC, $matches);
None of this will return the following into the correct order sorted ascending by suggested_play_order.
var_dump($matches); returns:
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) "" } } }
any ideas what I might be doing wrong here?
Advertisement
Answer
Each array has a match
index containing an array that contains suggested_play_order
. You can use array_column
twice extracting all arrays under the match
indexes and then suggested_play_order
from there:
array_multisort(array_column(array_column($matches, "match"), "suggested_play_order"), $matches);
Also, passing null
as the second argument and an index as the third returns the entire array indexed by the third argument. Not what you want.