2015-02-10 81 views
0

希望我能正確解釋這一點。PHP如何組合多維數組,其值相同

我有一個多維數組得到這個數組結果:

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 
       ) 

     ) 
) 

任何想法?這個怎麼做。

感謝

+0

[Group am通過特定的值ultidimensional數組?](http://stackoverflow.com/questions/2189626/group-a-multidimensional-array-by-a-particular-value) – 2015-02-10 10:59:14

回答

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

得到這個錯誤:'語法錯誤意外的'eof'期待']'' – 2015-02-10 10:59:58

+0

@ Mr.上面的代碼似乎很好。檢查代碼是否已經錯過任何關閉']' – Lepanto 2015-02-10 11:07:40

+0

現在代碼正在工作,但是如何將此數組鍵'[0126] [1025]'更改爲[[0] [1]]。我想重置'proerty_id'鍵。 – 2015-02-10 11:14:14

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

如果這些數據來自數據庫,似乎是由subscription_plan_id訂購。 如果是這樣,您可以嘗試重新排序或設置ORDER BY property_id

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

,如果你需要一個索引數組,你可以這樣做:

$groupped_array = array_values($groupped_array); 

希望它有助於...