2012-04-19 97 views
0

我意識到還有其他幾個與此有關的問題 - 但它們對我來說都沒有意義,或者根本無法工作。按特定值排序多維數組(使用asort和rasort)

我有一個數組,通過使用asort()arsort(),可以按字母順序排列第一個值。我多維數組的一個例子可能是:

[{"Name":"Amber","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Bob","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Hans","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Jeff","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Michael","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0}] 

隨着使用asort()arsort()它是由"Name"字母順序排序或反向字母。現在我需要相同的功能,僅基於"dealType"

我已經嘗試了幾個已經發布在Stackoverflow上的例子,但我必須誤解他們,因爲他們不工作。我該如何做到這一點?

編輯 喬納森給出的字母排序正確的答案,有輕微的修改:

//the custom function to do our sort 
function cmp($a,$b){ 
    //get which string is less or 0 if both are the same 
    $cmp = strcasecmp($a->dealType, $b->dealType); 
    //if the strings are the same, check name 
    return $cmp; 
} 
//sort using a custom function 
usort($obj, 'cmp'); 

和下面這段代碼反向字母排序:

//the custom function to do our sort 
function cmp($a,$b){ 
    //get which string is less or 0 if both are the same 
    $cmp = strcasecmp($b->dealType, $a->dealType); 
    //if the strings are the same, check name 
    return $cmp; 
} 
//sort using a custom function 
usort($obj, 'cmp'); 

回答

2

您應該使用usort這將允許您使用自定義功能進行比較,以確定排序時價值是第一位的。

類似:

<?php 
//the data you supplied. normally just an array 
$data = array (0 => array ('Name' => 'Amber', 'date' => '', 'dealType' => 'deal1', 'id' => '***@***.com', 'registered' => 0,), 1 => array ('Name' => 'Bob', 'date' => '', 'dealType' => 'deal5', 'id' => '***@***.com', 'registered' => 0,), 2 => array ('Name' => 'Hans', 'date' => '', 'dealType' => 'deal3', 'id' => '***@***.com', 'registered' => 0,), 3 => array ('Name' => 'Jeff', 'date' => '', 'dealType' => 'deal2', 'id' => '***@***.com', 'registered' => 0,), 4 => array ('Name' => 'Michael', 'date' => '', 'dealType' => 'deal1', 'id' => '***@***.com', 'registered' => 0,),); 

//show what we got going into sort 
echo '<pre>'.print_r($data, 1).'</pre>'; 

function cmp($a,$b){ 
    //get which string is less or 0 if both are the same 
    $cmp = strcasecmp($a['dealType'], $b['dealType']); 
    //if the strings are the same, check name 
    if($cmp == 0){ 
     //compare the name 
     $cmp = strcasecmp($a['Name'], $b['Name']); 
    } 
    return $cmp; 
} 
//sort using a custom function 
usort($data, 'cmp'); 

//show what we got after sort 
echo '<pre>'.print_r($data, 1).'</pre>'; 
?> 

將在dealType第一,然後Name秒排序如果dealType是一樣的。如果您想要反向排序,則可以在調用strcasecmp()時調換$a$b的順序。

編輯:這個數據可能是從數據庫中提取的。如果是這樣,那麼只需ORDER BY的列。你可以這樣做:SELECT * FROM table ORDER BY dealType ASC,Name ASC

Edit2:更新了代碼,不使用匿名函數。你也可以查看代碼在ideone

+0

它是通過.NET WCF給我的,否則我會做你編輯說的。你上面的代碼給出了某種錯誤。 – mdance 2012-04-19 21:14:11

+0

舊版本的php( 2012-04-19 21:20:02

+0

我有版本5.3.3 – mdance 2012-04-19 21:22:35

0

我相信你正在尋找對於array_multisort()http://php.net/manual/en/function.array-multisort.php

例如:

<?php 
$ar = array(
     array("10", 11, 100, 100, "a"), 
     array( 1, 2, "2", 3, 1) 
    ); 
array_multisort($ar[0], SORT_ASC, SORT_STRING, 
       $ar[1], SORT_NUMERIC, SORT_DESC); 
var_dump($ar); 
?> 

來源:http://php.net/manual/en/function.array-multisort.php#example-4558

+0

'在array_multisort()'一直是一個謎我出於某種原因...這將是巨大的如果你能解釋如何使用它並使你的答案適應這個問題。 – rid 2012-04-19 20:39:23

+0

'array_multisort()'不工作...不排序任何東西。至少我如何設置:'array_multisort($ objVZA [1],SORT_DESC,SORT_STRING);' – mdance 2012-04-19 20:42:05

+0

@mdance,'array_multisort()* * *工作,只要你可以得到正確的咒語... – rid 2012-04-19 20:44:19

1

工作如果我正確理解你的問題,你有陣列(說)陣列(比如BS),並且希望通過他們一定的參考價值燒烤排序。如果這是正確的,我以前有同樣的問題,並制定了自定義函數稱爲line_sort。 如果你願意,你可以通過例如使用它:

$array=json_decode('[{"Name":"Amber","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Bob","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Hans","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Jeff","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Michael","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0}]'); 
$array=line_sort("dealType",$array); 

第一個值是要排序鍵,第二個是數組進行,這裏整理的功能代碼(對不起它不是英文) :

function line_sort($klic,$hodnoty) 
{ 
    if (!is_array($hodnoty)) 
    { 
    trigger_error("Second parameter has to be multidimensional array", E_USER_WARNING); 
    return; 
    } 
    for ($x=0;$x<count($hodnoty)-1;$x++) 
    { 
    for ($y=count($hodnoty)-1;$y>=$x;$y--) 
    { 
     $radek1=$hodnoty[$y]; 
     $radek2=$hodnoty[$y+1]; 
     if ((isset($radek1[$klic]) && isset($radek2[$klic])) && (intval($radek1[$klic]) > intval($radek2[$klic])) || (isset($radek2[$klic]) && !isset($radek1[$klic]))) 
     { 
     $hodnoty[$y]=$radek2; 
     $hodnoty[$y+1]=$radek1; 
     continue; 
     } 
     else if((isset($radek1[$klic]) && isset($radek2[$klic])) && (intval($radek1[$klic]) == intval($radek2[$klic]))) 
     { 
     if (strcmp($radek1[$klic],$radek2[$klic])>0) 
     { 
      $hodnoty[$y]=$radek2; 
      $hodnoty[$y+1]=$radek1; 
     } 
     } 
    } 
    } 
    return $hodnoty; 
} 

如果你想看到的輸出示例的此功能,看看這個:http://test.hanzlsoft.eu/