2013-01-09 1 views
1

저는 array_multisort 함수를 가지고 놀았지 만 가지고있는 배열로 동작 시키려면 고민 중입니다. 여기 어떻게 PHP로 다차원 배열을 2 개의 값으로 정렬합니까?

내가 정렬하려고 다중 차원 배열의 VAR 덤프입니다 :

array(2) { 
[1]=> array(6) { 
    ["totalprice"]=> float(103.32) 
    ["itemsprice"]=> float(83.33) 
    ["deliveryprice"]=> float(19.99) 
    ["qualityscore"]=> int(100) 
    ["reliabilityscore"]=> int(100) 
    ["itemtemplates"]=> array(4) { 
     [1]=> array(3) { 
      ["price"]=> float(374) 
      ["qty"]=> int(200) 
      ["name"]=> string(17) "English A2 Poster" 
     } 
     [3]=> array(3) { 
      ["price"]=> float(374) 
      ["qty"]=> int(500) 
      ["name"]=> NULL 
     } 
     [6]=> array(3) { 
      ["price"]=> float(83.333333333333) 
      ["qty"]=> int(100) 
      ["name"]=> string(16) "French A3 Poster" 
     } 
     [5]=> array(3) { 
      ["price"]=> float(83.333333333333) 
      ["qty"]=> int(5000) ["name"]=> NULL 
     } 
    } 
} 
[2]=> array(6) { 
    ["totalprice"]=> float(103.32) 
    ["itemsprice"]=> float(83.33) 
    ["deliveryprice"]=> float(19.99) 
    ["qualityscore"]=> int(80) 
    ["reliabilityscore"]=> int(100) 
    ["itemtemplates"]=> array(4) { 
     [1]=> array(3) { 
      ["price"]=> float(374) 
      ["qty"]=> int(200) 
      ["name"]=> string(17) "English A2 Poster" 
     } 
     [3]=> array(3) { 
      ["price"]=> float(374) 
      ["qty"]=> int(500) 
      ["name"]=> NULL 
     } 
     [6]=> array(3) { 
      ["price"]=> float(83.333333333333) 
      ["qty"]=> int(100) 
      ["name"]=> string(16) "French A3 Poster" 
     } 
     [5]=> array(3) { 
      ["price"]=> float(83.333333333333) 
      ["qty"]=> int(5000) ["name"]=> NULL 
     } 
    } 
} 
[3]=> array(6) { 
    ["totalprice"]=> float(83.32) 
    ["itemsprice"]=> float(63.33) 
    ["deliveryprice"]=> float(19.99) 
    ["qualityscore"]=> int(60) 
    ["reliabilityscore"]=> int(40) 
    ["itemtemplates"]=> array(4) { 
     [1]=> array(3) { 
      ["price"]=> float(374) 
      ["qty"]=> int(200) 
      ["name"]=> string(17) "English A2 Poster" 
     } 
     [3]=> array(3) { 
      ["price"]=> float(374) 
      ["qty"]=> int(500) 
      ["name"]=> NULL 
     } 
     [6]=> array(3) { 
      ["price"]=> float(83.333333333333) 
      ["qty"]=> int(100) 
      ["name"]=> string(16) "French A3 Poster" 
     } 
     [5]=> array(3) { 
      ["price"]=> float(83.333333333333) 
      ["qty"]=> int(5000) ["name"]=> NULL 
     } 
    } 
} 

}

내가 전체 가격 ASC에 의해 다음 품질 평가 점수 DESC을 기준으로 정렬 할 필요가

.

$sorted = array_multisort($array['totalprice'], SORT_ASC, SORT_NUMERIC, 
    $array['qualityscore'], SORT_NUMERIC, SORT_DESC); 

는 불행하게도 문제가 해결되지 않습니다

나는 다음과 같은 시도했습니다. 누구든지이 기능에 좀 더 능숙하고 어디서 잘못 될지 알 수 있습니까? 또는이 기능에 대한 간단한 대안이 있다면?

미리 감사드립니다.

답변

0

시도 다음 코드를

$totalprices = array(); 
$qualityscores = array(); 
foreach ($array as $value) { 
    $totalprices[] = $value['totalprice']; 
    $qualityscores[] = $value['qualityscore']; 
} 

array_multisort($totalprices, SORT_ASC, $qualityscores, SORT_DESC, $array); 

RTM :이

답장을 보내
+0

감사합니다, 나는 설명서를 읽어 않았고 그런 식으로 일을 살펴 보았다 http://php.net/manual/en/function.array-multisort.php 그러나 이것은 단지 나에게 총 목록을 제공 가격 및 수량을 포함하지만 다른 모든 연관 데이터는 아닙니다. 정렬해야하지만 동일한 형식으로 유지해야한다고 언급 했어야합니다. –

+0

죄송합니다.하지만이 방법을 테스트 한 결과 전체 $ array가 결과로 반환되었습니다. 총 가격과 품질뿐만 아니라 –

+0

맞습니다. 나는 내가 전에 무엇을하고 있었는지 확신하지 못한다! :) 감사! –

3

usort() 기능을 사용하십시오.

분명히 자체 정렬 기능을 작성해야합니다. 이 기능을 usort()으로 전달하면 필요한 기준으로 정렬 할 수 있습니다.

정렬 기능을 정의하는 방법은 위의 링크 된 매뉴얼 페이지를 참조하십시오.

관련 문제