2012-06-20 328 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字符串,应事先完成,理想情况下在数据输入时间。