PHP 如何对值相同的多维数组进行分组

PHP how to group a multidimensional array which value is same

希望我能正确解释这一点。

我有一个多维数组得到这个数组结果:

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

我想按 property_id 键分组:

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
                )

        )
)

有什么想法吗?怎么做。

谢谢

大致是这样的:

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

为了将来的可重用性,最好使用 function()

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

几周前我已经为我的项目编写了它,事实证明它运行良好。您不必使用 $remove_keyflatten_output.

用法:

var_dump(array_multi_group_by_key($input_array, 'property_id'));

您可以使用 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

如果这些数据来自数据库,似乎是由 subscription_plan_id 订购的。 如果是这样,您可以尝试重新排序或设置 ORDER BY property_id.

试试这个功能:

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

你这样称呼它:

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

如果你需要一个 INDEXED 数组,你可以这样做:

$groupped_array = array_values($groupped_array);

希望对你有帮助...