2017-10-28 138 views
0

我想合併一個鍵內兩個鍵的兩個值。 數組是這樣的:如何將一個數組中的兩個鍵合併成一個鍵?

[PromotionIds] => Array (
    [PromotionId] => Array (
     [0] => Amazon PLCC Free-Financing Universal Merchant MP-rachmit-1507891499711 
     [1] => Amazon PLCC Free-Financing Universal Merchant Script-1507895115492 
     ) 
    ) 

,但我想合併[0][1]鑰匙插入鑰匙[PromotionID]

[PromotionIds] => Array (
    [PromotionId] => Amazon PLCC Free-Financing Universal Merchant MP-rachmit-1507891499711,Amazon PLCC Free-Financing Universal Merchant Script-1507895115492 
    ) 
+0

試試Array_combine。 http://php.net/manual/en/function.array-combine.php – TarangP

回答

0

我認爲implode方法可以幫助你:

<?php 

$a = [ 
    123 => [ 
     "String1", 
     "String2" 
    ], 
    234 => [ 
     "String3", 
     "String4", 
     "String5" 
    ] 
]; 

foreach($a as $promotionId => $items) { 
    $a[$promotionId] = implode($items, ','); 
} 

var_dump($a); 


//array(2) { 
// [123] => 
//  string(15) "String1,String2" 
// [234] => 
//  string(23) "String3,String4,String5" 
//} 
+0

感謝您記住我'implode()'它幫助了我很多 –

+0

@PrAtikLochawala歡迎您!請接受我的回答,如果它解決了你的問題。 –

0

試試這個方法

$array = ['PromotionIds' => Array (
    'PromotionId' => Array (
     0 => 'Amazon PLCC Free-Financing Universal Merchant MP-rachmit-1507891499711', 
     1 => 'Amazon PLCC Free-Financing Universal Merchant Script-1507895115492' 
     ) 
    )]; 
$result = []; 

foreach ($array['PromotionIds'] as $key => $value) { 
    $result[$key]=implode(',', $value); 
} 

echo "<pre>"; 
print_r($result); 
echo "</pre>"; 
exit; 

結果會如您所料。

相關問題