2016-06-01 66 views
-3

我有2個陣列如下所示,如果我想array[0]array[1]ht_cat_name是相同的。所以ht_category是2陣列常見的。所以ht_category基於合併ht_amenity_name。如何合併陣列

Array 
(
    [0] => Array 
     (
      [ht_amenity_id] => 1 
      [ht_amenity_name] => Central Air Conditioning 
      [ht_category] => 1 
      [ht_cat_name] => General 
     ) 

    [1] => Array 
     (
      [ht_amenity_id] => 2 
      [ht_amenity_name] => Facilities for disabled guests 
      [ht_category] => 1 
      [ht_cat_name] => General 
     ) 

    [2] => Array 
     (
      [ht_amenity_id] => 3 
      [ht_amenity_name] => Climate control 
      [ht_category] => 2 
      [ht_cat_name] => Services 
     ) 
) 

我想合併這樣的設施名稱。

Array 
(
    [0] => Array 
     (
      [ht_amenity_id][0] => 1 
      [ht_amenity_id][1] => 2 
      [ht_amenity_name][0] => Central Air Conditioning 
      [ht_amenity_name][1] => Facilities for disabled guests 
      [ht_category] => 1 
      [ht_cat_name] => General 
     ) 
    [1] => Array 
     (
      [ht_amenity_id] => 3 
      [ht_amenity_name] => Climate control 
      [ht_category] => 2 
      [ht_cat_name] => Services 
     ) 

) 
+2

哪裏是代碼,你試過嗎?請添加您爲此嘗試的代碼.... – Vickrant

+0

$ amen = DB :: select(「SELECT'h'.'ht_amenity_id','h'.'ht_amenity_name','h'.'ht_category','a '.'ht_cat_name' FROM'abserve_hotel_amenities' as'h'將abserve_hotel_categories加入'a' ON'a'.'ht_cat_id' ='h'.'ht_category'「); \t \t \t $ am = array(); \t \t \t的foreach($阿門爲$密鑰=> $值){ \t \t \t $是[] =(get_object_vars($值)); \t \t \t} \t \t \t $用戶=陣列(); \t \t \t的foreach($的空房爲$密鑰=> $值){ \t \t \t $用戶[] =(get_object_vars($值)); \t \t \t} – murugesh

+0

sry,$ amen = DB :: select(「SELECT'h'.ht_amenity_id','h'.ht_amenity_name','h'.ht_category','a'.'ht_cat_name' FROM 'abserve_hotel_amenities' as'h'將abserve_hotel_categories加入'a' ON'a'.'ht_cat_id' ='h'。'ht_category'「); \t \t \t $ AM =陣列(); \t \t \t的foreach($阿門爲$密鑰=> $值){ \t \t \t $是[] =(get_object_vars($值) ); \t \t \t} \t \t \t $用戶=陣列(); \t \t \t的foreach($的空房爲$密鑰=> $值){ \t \t \t $ user [] =(get_object_vars($ value)); \t \t \t} – murugesh

回答

0

如果你不能改變你怎麼弄的數據(即它來自一個API,或者您不能修改SQL查詢)你最好的選擇將是一個集流水線庫(純PHP解決辦法是兩個嵌套foreaches我不喜歡太多

這應該爲你工作(使用Knapsack寫的):

$data = [ 
    [ 
     "ht_amenity_id" => 1, 
     "ht_amenity_name" => "Central Air Conditioning", 
     "ht_category" => 1, 
     "ht_cat_name" => "General", 
    ], 

    [ 
     "ht_amenity_id" => 2, 
     "ht_amenity_name" => "Facilities for disabled guests", 
     "ht_category" => 1, 
     "ht_cat_name" => "General", 
    ], 

    [ 
     "ht_amenity_id" => 3, 
     "ht_amenity_name" => "Climate control", 
     "ht_category" => 2, 
     "ht_cat_name" => "Services", 
    ] 
]; 

$transformed = Collection::from($data) 
    ->groupByKey('ht_category') 
    ->map(function (Collection $category, $categoryId) { 
     return [ 
      "ht_amenity_id" => $category->extract('ht_amenity_id')->toArray(), 
      "ht_amenity_name" => $category->extract('ht_amenity_name')->toArray(), 
      "ht_category" => $categoryId, 
      "ht_cat_name" => $category->extract('ht_cat_name')->first(), 
     ]; 
    }) 
    ->toArray(); 

收集管道庫: