将数组插入另一个PHP

时间:2019-01-16 01:25:41

标签: php arrays json

我有两个阵列-卡和令牌,我想将一个放入另一个阵列:

{
    "cards": [
    {
        "CardNumber": "xxxxxx******xxxx",
        "Holder": "EUEUEUEUEUEU",
        "ExpirationDate": "xx/xxxxx"
    },
    {
        "CardNumber": "xxxxxx******xxxx",
        "Holder": "EU",
        "ExpirationDate": "xx/xxxxx"
    }
]
}

{
"tokens": [
    {
        "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    },
    {
        "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
]

如何将第二个数组插入第一个数组,如下所示:

{
"cards": [
    {
        "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "CardNumber": "xxxxxx******xxxx",
        "Holder": "EUEUEUEUEUEU",
        "ExpirationDate": "xx/xxxxx"
    },
    {
        "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "CardNumber": "xxxxxx******xxxx",
        "Holder": "EU",
        "ExpirationDate": "xx/xxxxx"
    }
]
}

我正在使用以下代码获取纸牌阵列。 $ token返回令牌值:

    foreach ($token_values as $token) {
        $request[] = getCards($merchantId, $merchantKey, $token);
    }
    $arrayJSON['cards'] = $request;
    echo json_encode($arrayJSON);

1 个答案:

答案 0 :(得分:4)

您应该能够在token循环内添加foreach值:

foreach ($token_values as $token) {
    $request[] = array_merge(array('token' => $token), getCards($merchantId, $merchantKey, $token));
}