2011-09-30 65 views
3

我有一個像這樣的幾個數組。php多維排序

Array 
(
[state] => Array 
    (
     [0] => WA 
     [1] => CA 
     [2] => CA 
     [3] => NV 
     [4] => MO 
     [5] => CA 
     [6] => CA 
     [7] => CA 
     [8] => CT 
     [9] => FL 
     [10] => FL 
     [11] => ID 
     [12] => ID 
     [13] => IN 
     [14] => MN 
     [15] => MN 
     [16] => NE 
     [17] => NY 
     [18] => TX 
     [19] => TX 
     [20] => WI 
    ) 



[counties] => Array 
    (
     [0] => King, Snohomish 
     [1] => Contra Costa 
     [2] => Los Angeles 
     [3] => Clark 
     [4] => Jackson 
     [5] => Tulare 
     [6] => Sacramento 
     [7] => Riverside 
     [8] => New Haven 
     [9] => Pinellas 
     [10] => Lake 
     [11] => Canyon 
     [12] => Ada 
     [13] => Tippecanoe, White, Carroll 
     [14] => Crow Wing, Cass 
     [15] => Blue Earth 
     [16] => Douglas 
     [17] => Rockland 
     [18] => Webb 
     [19] => Harris 
     [20] => Waukesha, Milwaukee, Washington 


    ) 

[zipcodes] => Array 
    (
     [0] => 98004, 98005, 98006, 98007, 98008, 98011, 98012, 98102, 98105, 98112, 98136, 98025, 98033, 98034, 98083 

     [1] => 94530, 94804, 94805, 94803, 94806, 94564, 94547 

     [2] => 91381, 91384, 91354, 91355, 91321, 91387, 91351, 91390, 91350 
     [3] => 89002, 89009, 89011, 89012, 89014, 89015, 89016, 89128, 89048, 89052, 89053, 89060, 89061, 89074, 94588, 89102, 89105, 89108, 89109, 89111, 89112 
     [4] => 64055, 64056, 64057, 64052, 64064, 64050, 64058, 64014, 64015, 64029, 64063, 64081, 64082, 64086, 64133 

     [5] => 
     [6] => 
     [7] => 
     [8] => 
     [9] => 
     [10] => 
     [11] => 
     [12] => 
     [13] => 
     [14] => 
     [15] => 
     [16] => 
     [17] => 
     [18] => 
     [19] => 
     [20] => 
    ) 
) 

我想按郵編排序,然後按州,然後按縣。我遇到的問題是通過字符串中的第一個zip以升序顯示郵編。所以訂單將是

64055, 64056, ... 

89002, 89009, ... 

91381, 91384, ... 

94530, 94804, ... 

這是我目前的結果。

WA,TL-WA-150,150,King, Snohomish,98004, 98005, 98006, 98007, 98008, 98011, 98012, 98102, 98105, 98112, 98136, 98025, 98033, 98034, 98083 

CA,HD-CA-125,125,Contra Costa,94530, 94804, 94805, 94803, 94806, 94564, 94547 

CA,DH-CA-125,150,Los Angeles,91381, 91384, 91354, 91355, 91321, 91387, 91351, 91390, 91350 

NV,CM1-NV-150,150,Clark,89002, 89009, 89011, 89012, 89014, 89015, 89016, 89128, 89048, 89052, 89053, 89060, 89061, 89074, 94588, 89102, 89105, 89108, 89109, 89111, 89112 

MO,CJ-MO-150,150,Jackson,64055, 64056, 64057, 64052, 64064, 64050, 64058, 64014, 64015, 64029, 64063, 64081, 64082, 64086, 64133 

CA,GR6-CA-150,150,Tulare, 

CA,SSJ-CA-150,150,Sacramento, 

CA,LM1-CA-150,150,Riverside, 

CT,TAMRM-CT-150,150,New Haven, 

FL,GG-FL-150,150,Pinellas, 

我的問題是如何獲得郵編排序升序?

我現在正在使用它進行排序。

sortDataSet($difarray, 'zipcodes', SORT_DESC, SORT_NUMERIC, 'state', SORT_ASC, SORT_STRING,'counties', SORT_DESC, SORT_STRING); 

function sortDataSet(&$dataSet) { 
$args = func_get_args(); 
$callString = 'array_multisort('; 
$usedColumns = array(); 
for($i = 1, $count = count($args); $i < $count; ++$i) { 
    switch(gettype($args[$i])) { 
     case 'string': 
      $callString .= '$dataSet[\''.$args[$i].'\'], '; 
      array_push($usedColumns, $args[$i]); 
      break; 
     case 'integer': 
      $callString .= $args[$i].', '; 
      break; 
     default: 
      throw new Exception('expected string or integer, given '.gettype($args[$i])); 
    } 
} 
foreach($dataSet as $column => $array) { 
    if(in_array($column, $usedColumns)) continue; 
    $callString .= '$dataSet[\''.$column.'\'], '; 
} 
eval(substr($callString, 0, -2).');'); 
} 

回答

1

下面是代碼,可以幫助你

$array = array(
    'zipcodes' => array(
     '98004, 98005, 98006, 98007, 98008, 98011, 98012, 98102, 98105, 98112, 98136, 98025, 98033, 98034, 98083', 
     '94530, 94804, 94805, 94803, 94806, 94564, 94547', 
     '91381, 91384, 91354, 91355, 91321, 91387, 91351, 91390, 91350', 
     '89002, 89009, 89011, 89012, 89014, 89015, 89016, 89128, 89048, 89052, 89053, 89060, 89061, 89074, 94588, 89102, 89105, 89108, 89109, 89111, 89112', 
     '64055, 64056, 64057, 64052, 64064, 64050, 64058, 64014, 64015, 64029, 64063, 64081, 64082, 64086, 64133', 
    ), 
    'state' => array(
     'WA', 'CA', 'FL' 
    ), 
    'counties' => array(
     'Lake', 'King, Snohomish' 
    ) 
); 

foreach ($array as $key => $value) { 
    sort($array[$key]); // sort array from lowest to highest http://nz.php.net/manual/en/function.sort.php 
    // or reverse sort using arsort http://nz.php.net/manual/en/function.arsort.php 
} 

可以使用$鍵來決定使用哪種類型的功能

foreach ($array as $key => $value) { 
    if ($key == 'zipcodes') { 
     sort($array[$key]); 
    } else { 
     arsort($array[$key]); 
    } 
} 
+0

感謝您的輸入。我考慮用你的建議修改我的代碼,但不幸的是,它不會讓我得到我所需要的。 – Tom

+0

那麼,你可以發佈更多關於你在做什麼的細節。 – satrun77

4

所以我假設你有一個三個數組,狀態,縣和郵編的數組。 我還假設你的包含這些數組的數組被稱爲$data,所以在那裏用你的數組名稱替換。

我相信你的原始數組是這樣設置的。

$data = array(
'state' => array 
    (
    'WA', 
    'CA', 
    'CA', 
    'NV', 
    'MO', 
    'CA', 
    'CA', 
    'CA', 
    'CT', 
    'FL', 
    'FL', 
    'ID', 
    'ID', 
    'IN', 
    'MN', 
    'MN', 
    'NE', 
    'NY', 
    'TX', 
    'TX', 
    'WI' 
), 

'counties' => array 
(
    'King, Snohomish', 
    'Contra Costa', 
    'Los Angeles', 
    'Clark', 
    'Jackson', 
    'Tulare', 
    'Sacramento', 
    'Riverside', 
    'New Haven', 
    'Pinellas', 
    'Lake', 
    'Canyon', 
    'Ada', 
    'Tippecanoe, White, Carroll', 
    'Crow Wing, Cass', 
    'Blue Earth', 
    'Douglas', 
    'Rockland', 
    'Webb', 
    'Harris', 
    'Waukesha, Milwaukee, Washington' 


), 

'zipcodes' => array 
(
    '98004, 98005, 98006, 98007, 98008, 98011, 98012, 98102, 98105, 98112, 98136, 98025, 98033, 98034, 98083', 
    '94530, 94804, 94805, 94803, 94806, 94564, 94547', 
    '91381, 91384, 91354, 91355, 91321, 91387, 91351, 91390, 91350', 
    '89002, 89009, 89011, 89012, 89014, 89015, 89016, 89128, 89048, 89052, 89053, 89060, 89061, 89074, 94588, 89102, 89105, 89108, 89109, 89111, 89112', 
    '64055, 64056, 64057, 64052, 64064, 64050, 64058, 64014, 64015, 64029, 64063, 64081, 64082, 64086, 64133', 
    '','','','','','','','','','','','','','','','' 
)); 

所以,這是我的解決方案:

//make our own array with state, counties, and zips living together. 
$sortData=array(); 
foreach($data['state'] as $key =>$thisState){ 

//first sort the zipcode string itself. 
$zipcodeArray = explode(", ",$data['zipcodes'][$key]); 
sort($zipcodeArray,SORT_NUMERIC); 
$zipcodeString = implode(", ",$zipcodeArray); 

$pusharray=array('state'=>$thisState,'county'=>$data['counties'][$key],'zipcode'=>$zipcodeString); 
array_push($sortData,$pusharray); 

}//foreach 

$states=array(); 
$counties=array(); 
$zipcodes=array(); 
foreach ($sortData as $key => $row) { 

    $states[$key] = $row['state']; 
    $counties[$key] = $row['county']; 
    $zipcodes[$key] = $row['zipcode']; 

}// 
$index=0; 

array_multisort($zipcodes, SORT_NUMERIC, SORT_ASC, $states, SORT_ASC, $counties, SORT_ASC, $sortData); 


function blank($var) 
{ 
if (empty($var['zipcode'])){return true;}else{return false;} 
} 

function zipcodeString($var) 
{ 
if (!(empty($var['zipcode']))){return true;}else{return false;} 
} 


$noZipcodeChunk=(array_filter($sortData, "blank")); 
$zipcodeChunk=(array_filter($sortData, "zipcodeString")); 
$finalArray = array_merge($zipcodeChunk,$noZipcodeChunk); 



foreach ($finalArray as $datum){ 
    echo $datum['state'].' '.$datum['county'].' '.$datum['zipcode'].'<br>'; 

} 

而且我認爲這是你地區尋找:

MO Jackson 64014, 64015, 64029, 64050, 64052, 64055, 64056, 64057, 64058, 64063, 64064, 64081, 64082, 64086, 64133 
NV Clark 89002, 89009, 89011, 89012, 89014, 89015, 89016, 89048, 89052, 89053, 89060, 89061, 89074, 89102, 89105, 89108, 89109, 89111, 89112, 89128, 94588 
CA Los Angeles 91321, 91350, 91351, 91354, 91355, 91381, 91384, 91387, 91390 
CA Contra Costa 94530, 94547, 94564, 94803, 94804, 94805, 94806 
WA King, Snohomish 98004, 98005, 98006, 98007, 98008, 98011, 98012, 98025, 98033, 98034, 98083, 98102, 98105, 98112, 98136 
CA Riverside 
CA Sacramento 
CA Tulare 
CT New Haven 
FL Lake 
FL Pinellas 
ID Ada 
ID Canyon 
IN Tippecanoe, White, Carroll 
MN Blue Earth 
MN Crow Wing, Cass 
NE Douglas 
NY Rockland 
TX Harris 
TX Webb 
WI Waukesha, Milwaukee, Washington 
+0

這就是我所需要的,但我需要以64055開頭並以98004結尾的郵編。 – Tom

+0

我編輯了代碼及其結果輸出,是嗎? –

+0

我在錯誤的地方做了字符串本身的郵編。一探究竟。 –