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;
                                }
                            }
                        }
                    }

谢谢!

4

3 回答 3

3

取自 Johan 的一些修改:

  • 如果已经使用修剪,请避免使用 ltrim
  • 放入一个函数,我可以使用临时变量来保持可读性
  • 添加一个 final [],以避免令人讨厌的 $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;
}
于 2012-06-29T14:03:22.650 回答
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,但为了清楚起见,我决定将它们排除在外。

于 2012-06-29T09:32:54.590 回答
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');
}

您的数据结构依赖于同一天不可能有两个事务的假设,这是一个有点危险的假设。同样使用位置作为键也很不理想(因为拼写、位置变化等)——除非它真的应该对事物进行分组,比如纸质邮件。

数据清理,如trimming 字符串,应该事先完成,最好在数据输入时完成。

于 2012-06-29T09:54:27.303 回答