를 기반으로 배열을 정렬하려고 key=>value
하지만이 작업을 수행 할 수 없습니다. 내가 해결하고있는 코드는 다음과 같습니다.
$arr = [
'183034' => 9,
'183033' => 6,
'183032' => 3,
'183002' => null,
'182973' => null,
'182971' => null,
'182969' => null,
'182999' => null,
'182997' => null,
'182995' => null,
'182962' => null,
'182948' => null
];
$arrTemp = [];
foreach($arr as $key => $value) {
$arrTemp[$key] = $value;
}
array_multisort($arrTemp, SORT_NUMERIC, $arr);
var_export($arrTemp);
출력은 다음과 같습니다.
#php test.php
array (
0 => NULL,
1 => NULL,
2 => NULL,
3 => NULL,
4 => NULL,
5 => NULL,
6 => NULL,
7 => NULL,
8 => NULL,
9 => 3,
10 => 6,
11 => 9,
)
그러나 나는 다음과 같은 것을 기대합니다.
array (
'183002' => null,
'182973' => null,
'182971' => null,
'183032' => 3,
'182969' => null,
'182999' => null,
'183033' => 6,
'182997' => null,
'182995' => null,
'183034' => 9,
'182962' => null,
'182948' => null
);
여기서 value는 항목을 이동해야하는 위치를 정의합니다. 즉, 단어로 예를 들어 보겠습니다. '183032' => 3
이 항목은 결과 배열에서 위치 3을 유지하므로 동일한 배열 순서를 유지하지만 출력 배열에서 알 수 있듯이 해당 항목을 위치 3으로 이동해야합니다. '183033' => 6
이것이 위치 6을 유지하는 위치 와 동일 하므로 전체 배열을 재정렬하여 위치 6으로 이동합니다. 나에게 도움을 줄 수 있습니까?
최신 정보
원본 $arr
을 다음과 같이 변경하면 어떻게됩니까?
$arr = [
'183034' => ['sort_position' => 9],
'183033' => ['sort_position' => 5],
'183032' => ['sort_position' => 3],
'183002' => [],
'182973' => [],
'182971' => [],
'182969' => [],
'182999' => [],
'182997' => [],
'182995' => [],
'182962' => [],
'182948' => []
];
거의 동일하지만 이것은 배열이 어떻게 보이는지 (예제 일뿐)와 배열 []
에는 다른 키 가 있어야합니다 sort_position
. 관련성이 있기 때문에 여기에 쓰지 않습니다 .
$arr = [
'183034' => ['sort_position' => 9],
'183033' => ['sort_position' => 5],
'183032' => ['sort_position' => 3],
'183002' => [],
'182973' => [],
'182971' => [],
'182969' => [],
'182999' => [],
'182997' => [],
'182995' => [],
'182962' => [],
'182948' => []
];
$count = count($arr);
$tmp=[];
//sort by key descending
krsort($arr);
foreach($arr as $key=>$val){
//if element has a sort position
if(isset($val['sort_position'])){
//save it in tmp, indexed by sort position
$tmp[$val['sort_position']]=$val;
//and remove it from original array
unset($arr[$key]);
}
}
//Note $arr now only contains elements without sort position
$out=[];
//build new array of same length as original
for($i=0; $i < $count; $i++){
//if there is an element in temp with this sort position, use it
//else grab the next one from the non sort position elements
$out[] = isset($tmp[$i])? $tmp[$i] : array_shift($arr);
}
var_dump($out);
편집은 배열 키의 중요성을 놓쳤다는 것을 깨달았습니다. 결국 실제로 키가 필요하지 않을 수도 있지만 완전성을 위해 여기에 키도 보존하는 수정 된 솔루션이 있습니다.
$count = count($arr);
$has_sortorder=[];
$no_sortorder=[];
krsort($arr);
foreach($arr as $key=>$val){
if(isset($val['sort_position'])){
$has_sortorder[$val['sort_position']]=[$key, $val];
}else{
$no_sortorder[]=[$key, $val];
}
}
$out=[];
for($i=0; $i < $count; $i++){
if(isset($has_sortorder[$i])){
$out[$has_sortorder[$i][0]] = $has_sortorder[$i][1];
}else{
$element = array_shift($no_sortorder);
$out[$element[0]] = $element[1];
}
}
var_dump($out);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다