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

时间:2015-02-10 10:50:24

标签: php arrays multidimensional-array

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

我有一个获取此数组结果的多维数组:

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
                )

        )
)

有什么想法吗?怎么做。

由于

5 个答案:

答案 0 :(得分:2)

大致相同:

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

答案 1 :(得分:0)

最好使用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'));

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

尝试此功能:

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

希望它有所帮助......