이 기사는 다음 PHP 배열 분류 방법을 소개합니다.
분류 php 다차원 배열
및
함수를 사용합니다. 이것은 최종 결과에 영향을 미칩니다. 우리가 통과하는 는 하강 순서로 정렬됩니다. 이것은 그녀가 가장 높은 점수를 받았기 때문에다시 말해 배열은 내림차순으로 배열됩니다. 그런 다음 다른 배열의 값은 해당 순서와 일치하도록 재 배열됩니다. 그러므로 순서는 amanda
,등이됩니다.
가 같은 점수를 가지고 있음을 알 수 있습니다. 따라서 그들의 최종 위치는 건강에 의해 결정되며, 이는 오름차순으로 정렬되어야합니다. Monty의 건강은 adam
보다 낮으므로 array_multisort()
Adam
amanda
,$players = [ [ 'name' => 'Adam', 'score' => 70, 'health' => 80 ], [ 'name' => 'Joey', 'score' => 60, 'health' => 90 ], [ 'name' => 'Monty', 'score' => 70, 'health' => 45 ], [ 'name' => 'Andrew', 'score' => 90, 'health' => 80 ], [ 'name' => 'Sally', 'score' => 60, 'health' => 85 ], [ 'name' => 'Amanda', 'score' => 98, 'health' => 50 ], [ 'name' => 'James', 'score' => 50, 'health' => 50 ] ]; $p_score = array_column($players, 'score'); $p_health = array_column($players, 'health'); array_multisort($p_score, SORT_DESC, $p_health, SORT_ASC, $players);andrew ,
monty , adam 등이됩니다. 다른 모든 값 충돌은 비슷한 방식으로 해결됩니다. 배열을 정렬 한 후 얻을 수있는 최종 결과는 다음과 같습니다.
$players = [ [ 'name' => 'Adam', 'score' => 70, 'health' => 80 ], [ 'name' => 'Joey', 'score' => 60, 'health' => 90 ], [ 'name' => 'Monty', 'score' => 70, 'health' => 45 ], [ 'name' => 'Andrew', 'score' => 90, 'health' => 80 ], [ 'name' => 'Sally', 'score' => 60, 'health' => 85 ], [ 'name' => 'Amanda', 'score' => 98, 'health' => 50 ], [ 'name' => 'James', 'score' => 50, 'health' => 50 ] ]; $p_score = array_column($players, 'score'); $p_health = array_column($players, 'health'); array_multisort($p_score, SORT_DESC, $p_health, SORT_ASC, $players);
를 별도로 사용하십시오. sort()
print_r($players); /* Array ( [0] => Array ( [name] => Amanda [score] => 98 [health] => 50 ) [1] => Array ( [name] => Andrew [score] => 90 [health] => 80 ) [2] => Array ( [name] => Monty [score] => 70 [health] => 45 ) [3] => Array ( [name] => Adam [score] => 70 [health] => 80 ) [4] => Array ( [name] => Sally [score] => 60 [health] => 85 ) [5] => Array ( [name] => Joey [score] => 60 [health] => 90 ) [6] => Array ( [name] => James [score] => 50 [health] => 50 ) ) */
sort()
이 기사는 업데이트되었으며 Monty Shokeen의 기여를 포함합니다. Monty는 튜토리얼을 작성하고 새로운 JavaScript 라이브러리를 배우는 것을 좋아하는 풀 스택 개발자입니다.
위 내용은 PHP에서 배열을 정렬하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!