2012-06-20 400 views
2

我有這樣的數組。按兩個值分組多維數組

[11] => Array 
    (
     [transactioncurrencyid] => Array 
      (
       [!name] => US Dollar 
       [!] => {041E3DC9-D938-DD11-982C-0013724C58B7} 
      ) 

     [smi_processingmonth] => Array 
      (
       [!date] => 6/1/2011 
       [!time] => 2:27 PM 
       [!] => 2011-06-01T14:27:00-07:00 
      ) 

     [smi_cchistoryid] => {678C5036-9EAA-E111-88E0-00155D010302} 
     [smi_includeindeal] => Array 
      (
       [!name] => No 
       [!] => 0 
      ) 

        [smi_locationid] => Array 
      (
       [!name] => 1134 Hooksett Rd 
       [!] => {5CC1585B-91AA-E111-88E0-00155D010302} 
      ) 
    ) 

[12] => Array 
    (
     [transactioncurrencyid] => Array 
      (
       [!name] => US Dollar 
       [!] => {041E3DC9-D938-DD11-982C-0013724C58B7} 
      ) 


     [smi_processingmonth] => Array 
      (
       [!date] => 5/1/2011 
       [!time] => 2:27 PM 
       [!] => 2011-05-01T14:27:00-07:00 
      ) 

     [smi_cchistoryid] => {688C5036-9EAA-E111-88E0-00155D010302} 
     [smi_includeindeal] => Array 
      (
       [!name] => No 
       [!] => 0 
      ) 

     [smi_locationid] => Array 
      (
       [!name] => 1134 Hooksett Rd 
       [!] => {5CC1585B-91AA-E111-88E0-00155D010302} 
      ) 
    ) 

我怎樣才能將它們分組按位置ID然後通過smi_processingmonth

所以我得到這樣的

[1134 Hooksett Rd] => Array 
    (
     [ 5/1/2011] = array(
      [transactioncurrencyid] => Array 
      (
       [!name] => US Dollar 
       [!] => {041E3DC9-D938-DD11-982C-0013724C58B7} 
      ) 


      [smi_processingmonth] => Array 
       (
        [!date] => 5/1/2011 
        [!time] => 2:27 PM 
        [!] => 2011-05-01T14:27:00-07:00 
       ) 

      [smi_cchistoryid] => {688C5036-9EAA-E111-88E0-00155D010302} 
      [smi_includeindeal] => Array 
       (
        [!name] => No 
        [!] => 0 
       ) 

      [smi_locationid] => Array 
       (
        [!name] => 1134 Hooksett Rd 
        [!] => {5CC1585B-91AA-E111-88E0-00155D010302} 

      ) 
      ) 
     [1/1/2011] = array(
      [transactioncurrencyid] => Array 
      (
       [!name] => US Dollar 
       [!] => {041E3DC9-D938-DD11-982C-0013724C58B7} 
      ) 

     [smi_processingmonth] => Array 
      (
       [!date] => 6/1/2011 
       [!time] => 2:27 PM 
       [!] => 2011-06-01T14:27:00-07:00 
      ) 

     [smi_cchistoryid] => {678C5036-9EAA-E111-88E0-00155D010302} 
     [smi_includeindeal] => Array 
      (
       [!name] => No 
       [!] => 0 
      ) 

        [smi_locationid] => Array 
      (
       [!name] => 1134 Hooksett Rd 
       [!] => {5CC1585B-91AA-E111-88E0-00155D010302} 
      ) 
      ) 
    ) 

我已經試過

foreach($array as $keys) 
       { 

        $key = $keys['smi_processingmonth']['!date'];; 
        if (!isset($groups[$key])) 
        { 
         $groups[$key] = array($keys); 
        } else { 
         $groups[$key][] = $keys; 
        } 
       } 
       $newGroups = array(); 

       if(is_array($groups)) 
       { 
        foreach($groups as $cats => $values) 
        { 

         foreach($values as $itemValues){ 
          $st = rtrim(trim($itemValues['smi_locationid']['!'])); 
           $key = $st; 
           if (!isset($newGroups[$key])) 
           { 
            $newGroups[$key] = array($groups); 

           } else { 
            $newGroups[$key][] = $itemValues; 
           } 
          } 
         } 
        } 

謝謝!

回答

3

從約翰採取了一些修改:

  • 避免LTRIM如果已經使用功能修整
  • 說,我可以使用臨時變量保持可讀性
  • 添加最終[] ,以避免令人討厭的$ name/$ date碰撞,超出原始請求,但更安全
function compactArray($data) 
{ 
    $new_structure = array(); 
    foreach ($data as $row) 
     { 
     $name = trim($row['smi_locationid']['!name']); 
     $date = trim($row['smi_processingmonth']['!date']); 
     $new_structure[ $name ][ $date ][] = $row; 
    } 
    return $new_structure; 
} 
1

恩,我還沒有測試過。但是,是不是隻是:

$new_structure = array(); 
foreach ($data as $row) { 
    $key1 = rtrim(trim($row['smi_locationid']['!name'])); 
    $key2 = rtrim(trim($row['smi_processingmonth']['!date'])); 
    $new_structure[$key1][$key2] = $row; 
} 

這個代碼可以使用一些isset()函數S中,但我決定讓他們出去清晰。

0

下產生所需輸出數組,正確排序:

function transaction_datecmp($tran1, $tran2) 
{ 
    return strtotime($tran1['smi_processingmonth']['!']) - 
      strtotime($tran2['smi_processingmonth']['!']); 
} 

$output_arr = array(); 
foreach ($input_arr as $transaction) { 
    $location = $transaction['smi_locationid']  ['!name']; 
    $date  = $transaction['smi_processingmonth']['!date']; 
    $output_arr[$location][$date] = $transaction; 
} 
ksort($output_arr); // sorts by location 
foreach ($output_arr as &$transaction_arr) { 
    uasort($transaction_arr, 'transaction_datecmp'); 
} 

你的數據結構依賴於假設,即不能有在同一天,這是一個比較危險的假設兩筆交易。同樣使用位置作爲關鍵字也是遠遠不夠理想的(因爲拼寫,位置變化等) - 除非它真的應該對事物進行分組,如紙張郵寄。

數據清理,如trimm ing字符串,應事先完成,理想情況下在數據輸入時間。