按两个值对多维数组进行分组

时间:2012-06-20 18:47:15

标签: php

我有这样的数组。

[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 个答案:

答案 0 :(得分:3)

从约翰那里得到一些修改:

  • 如果已经使用了trim
  • ,请避免使用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 :(得分: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(),但为了清楚起见,我决定将它们保留下来。

答案 2 :(得分: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字符串,应该事先完成,理想情况是在数据输入时。