2

I have arrays like this.

[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}
            )
    )

How can i group them by location id then by smi_processingmonth

So I get something like this

[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}
            )
            )
    )

I have tried

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

Thanks!

3 Answers 3

3
+50

Taking from Johan with a few modifications:

  • avoid ltrim if already using trim
  • put in a function, I can use temporary variables to maintain readability
  • add a final [], to avoid nasty $name/$date collision, out of original request, but safer
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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Ehm, i have not tested it. But isn't it just:

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

This code could use some isset()s, but i decided to keep them out for clarity.

Comments

0

The following produces your desired output array, correctly sorted:

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

Your data structure relies on the assumption that there cannot be two transactions on the same day, which is a somewhat dangerous assumption. Also using a location as a key is far from ideal (because of spelling, location changes, etc.) - unless it really should group things, like for paper mailing.

Data cleanup, like trimming strings, should be done beforehand, ideally at data input time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.