如何将一个数组的两个键合并为一个键?

时间:2017-10-28 07:02:31

标签: php arrays

我想在一个键中合并两个键的两个值。 该数组如下所示:

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

2 个答案:

答案 0 :(得分: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"
//}

答案 1 :(得分:0)

试试这种方式

estimator.train

结果如您所愿。