,,
[의 뜻
'configuration_id'=> 9,
'ID'=> 1,
'OptionNumber'=> 2,
'OptionActive'=> 1,
'lastupdated'=> '2010-03-17 15:44:12'
],,
[의 뜻
'configuration_id'=> 8,
'ID'=> 1,
'OptionNumber'=> 1,
'OptionActive'=> 1,
'lastupdated'=> '2010-03-17 15:44:12'
],,
];
목표는이 배열이 '옵션 번호'키를 기반으로 점진적인 방식으로 정렬하는 것입니다. $ sortedArray = [ [의 뜻 'configuration_id'=> 8, 'ID'=> 1, 'OptionNumber'=> 1, 'OptionActive'=> 1, 'lastupdated'=> '2010-03-17 15:44:12' ],, [의 뜻 'configuration_id'=> 9, 'ID'=> 1, 'OptionNumber'=> 2, 'OptionActive'=> 1, 'lastupdated'=> '2010-03-17 15:44:12' ],, [의 뜻 'configuration_id'=> 10, 'ID'=> 1, 'OptionNumber'=> 3, 'OptionActive'=> 1, 'lastupdated'=> '2010-03-17 15:44:12' ],, ];
이 경우 비교 함수는 다음과 같이 정의 될 수 있습니다. return $ a
- $ B ; }
이 함수는 첫 번째 배열의 '옵션 번호'값을 두 번째 배열에서 빼고, 첫 번째 요소가 두 번째 요소 이전에 오려면 음수 값을 초래하고, 제로 값이 같으면, 첫 번째 요소가 두 번째 요소를이어야하는 경우 긍정적 값을 얻을 수 있습니다.
$array = [ [ 'configuration_id' => 10, 'id' => 1, 'optionNumber' => 3, 'optionActive' => 1, 'lastUpdated' => '2010-03-17 15:44:12' ], [ 'configuration_id' => 9, 'id' => 1, 'optionNumber' => 2, 'optionActive' => 1, 'lastUpdated' => '2010-03-17 15:44:12' ], [ 'configuration_id' => 8, 'id' => 1, 'optionNumber' => 1, 'optionActive' => 1, 'lastUpdated' => '2010-03-17 15:44:12' ], ];
usort ($ array, 'cmp_by_optionnumber');
$sortedArray = [ [ 'configuration_id' => 8, 'id' => 1, 'optionNumber' => 1, 'optionActive' => 1, 'lastUpdated' => '2010-03-17 15:44:12' ], [ 'configuration_id' => 9, 'id' => 1, 'optionNumber' => 2, 'optionActive' => 1, 'lastUpdated' => '2010-03-17 15:44:12' ], [ 'configuration_id' => 10, 'id' => 1, 'optionNumber' => 3, 'optionActive' => 1, 'lastUpdated' => '2010-03-17 15:44:12' ], ];
대체 구문 :
function cmp_by_optionNumber($a, $b) { return $a['optionNumber'] - $b['optionNumber']; }비교 함수 대신에 익명 함수를 사용할 수 있습니다. return $ a
- $ B
; });
function cmp_by_optionNumber($a, $b) { return $a['optionNumber'] - $b['optionNumber']; }php 7.0 이상 :
function cmp_by_optionNumber($a, $b) { return $a['optionNumber'] - $b['optionNumber']; }
우주선 연산자 & lt; = & gt; 오버플로/잘림 문제를 방지하기 위해 비교 함수에서 뺄셈 대신에 사용할 수 있습니다. $ a
& lt; = & gt; $ B; });
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3