2017-10-28 171 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; 

结果会如您所料。

相关问题