0

Hopefully, I can explain this correctly.

I have a multidimensional array getting this array result:

Array
(
    [0] => Array
        (
            [subscription_plan_id] => 36
            [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
            [property_id] => 1026
            [plan_name] => One Month
            [plan_month] => 1
            [start_date] => 2015-02-10 14:58:32
            [end_date] => 2015-03-10
            [active] => 1
            [parent_id] => 27
        )

    [1] => Array
        (
            [subscription_plan_id] => 17
            [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
            [property_id] => 1026
            [plan_name] => 1 Realistic Photo
            [plan_month] => 1
            [start_date] => 2015-02-10 14:58:32
            [end_date] => 2015-03-10
            [active] => 1
            [parent_id] => 16
        )

    [2] => Array
        (
            [subscription_plan_id] => 15
            [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
            [property_id] => 1026
            [plan_name] => Six months - at least twenty-four golden lists
            [plan_month] => 6
            [start_date] => 2015-02-10 14:58:32
            [end_date] => 2015-08-10
            [active] => 1
            [parent_id] => 12
        )

    [3] => Array
        (
            [subscription_plan_id] => 9
            [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
            [property_id] => 1025
            [plan_name] => One month
            [plan_month] => 1
            [start_date] => 2015-02-10 14:58:32
            [end_date] => 2015-03-10
            [active] => 1
            [parent_id] => 5
        )

    [4] => Array
        (
            [subscription_plan_id] => 3
            [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
            [property_id] => 1025
            [plan_name] => Three months
            [plan_month] => 3
            [start_date] => 2015-02-10 14:58:32
            [end_date] => 2015-05-10
            [active] => 1
            [parent_id] => 1
        )
)

and I want to group by property_id key:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [subscription_plan_id] => 36
                    [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
                    [property_id] => 1026
                    [plan_name] => One Month
                    [plan_month] => 1
                    [start_date] => 2015-02-10 14:58:32
                    [end_date] => 2015-03-10
                    [active] => 1
                    [parent_id] => 27
                )

            [1] => Array
                (
                    [subscription_plan_id] => 17
                    [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
                    [property_id] => 1026
                    [plan_name] => 1 Realistic Photo
                    [plan_month] => 1
                    [start_date] => 2015-02-10 14:58:32
                    [end_date] => 2015-03-10
                    [active] => 1
                    [parent_id] => 16
                )

            [2] => Array
                (
                    [subscription_plan_id] => 15
                    [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
                    [property_id] => 1026
                    [plan_name] => Six months - at least twenty-four golden lists
                    [plan_month] => 6
                    [start_date] => 2015-02-10 14:58:32
                    [end_date] => 2015-08-10
                    [active] => 1
                    [parent_id] => 12
                )

    [1] => Array
        (
            [0] => Array
                (
                    [subscription_plan_id] => 9
                    [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
                    [property_id] => 1025
                    [plan_name] => One month
                    [plan_month] => 1
                    [start_date] => 2015-02-10 14:58:32
                    [end_date] => 2015-03-10
                    [active] => 1
                    [parent_id] => 5
                )

            [1] => Array
                (
                    [subscription_plan_id] => 3
                    [transactionID] => WU3E5YRHHKMKMM3ZLLSJMNUWJM
                    [property_id] => 1025
                    [plan_name] => Three months
                    [plan_month] => 3
                    [start_date] => 2015-02-10 14:58:32
                    [end_date] => 2015-05-10
                    [active] => 1
                    [parent_id] => 1
                )

        )
)

Any idea? How to do this.

Thanks

Mr.Happy
  • 2,559
  • 8
  • 32
  • 67
  • possible duplicate of [Group a multidimensional array by a particular value?](http://stackoverflow.com/questions/2189626/group-a-multidimensional-array-by-a-particular-value) – Halayem Anis Feb 10 '15 at 10:59

5 Answers5

4

Roughly like this:

$groupedItems = array();

foreach($data as $item)
{
    $groupedItems[$item['property_id']][] = $item;
}

// See @Lepanto's comment below, this resets the keys to match output OP required: 
$groupedItems = array_values($groupedItems);
Dan Smith
  • 5,425
  • 29
  • 32
0

It's better to use a function() for future re-usability:

/**
 *
 * Group sub-arrays ( of multidimensional array ) by certain key
 * @return array
 *
 */
function array_multi_group_by_key($input_array, $key, $remove_key = false, $flatten_output = false)
{
    $output_array = [];
    foreach ($input_array as $array) {
        if ($flatten_output) {
            $output_array[$array[$key]] = $array;
            if ($remove_key) {
                unset($output_array[$array[$key]][$key]);
            }
        } else {
            $output_array[$array[$key]][] = $array;
            if ($remove_key) {
                unset($output_array[$array[$key]][0][$key]);
            }
        }
    }
    return $output_array;
}

I've written it for my project weeks ago and it's proven to be working fine. You don't have to use $remove_key nor flatten_output.

Usage:

var_dump(array_multi_group_by_key($input_array, 'property_id'));
Ilia Rostovtsev
  • 12,128
  • 10
  • 47
  • 80
0

You can use array_multisort php.net-array multisort

foreach ($data as $key => $row) {
        $property_id[$key] = $row['property_id'];
    }

    array_multisort($property_id, SORT_ASC, $data); //or SORT_DESC
Maky
  • 501
  • 5
  • 17
0

If those data comes from database, seems like ordered by subscription_plan_id. If so, you could try to reorder it or set ORDER BY property_id.

Tpojka
  • 6,435
  • 2
  • 25
  • 34
0

Try this function:

function group_multidim_array($my_array, $group_by){
    $o = array();
    foreach($my_array as $subarray){
        if(!is_array($subarray)){ continue; }
        if(!isset($subarray[$group_by])) $subarray[$group_by] = group_multidim_array($subarray, $group_by); // recursive
        if(!empty($subarray[$group_by])) $o[$subarray[$group_by]][] = $subarray;
    }
    return $o;
}

You call it like this:

$groupped_array = group_multidim_array($my_array, 'property_id');

if you need an INDEXED array you can do like this:

$groupped_array = array_values($groupped_array);

Hope It helps...

Federico
  • 1,236
  • 9
  • 12