0

I have two arrays - cards and tokens and I want to put one into another:

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

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

How to insert the second array into the first as follows:

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

I'm getting cards array with the following code. $token return the token values:

    foreach ($token_values as $token) {
        $request[] = getCards($merchantId, $merchantKey, $token);
    }
    $arrayJSON['cards'] = $request;
    echo json_encode($arrayJSON);
2
  • What format are these "arrays" in? They don't appear to be valid PHP. Please clarify your intent. Commented Jan 16, 2019 at 1:31
  • Sorry. I forgot some details. Edited! Commented Jan 16, 2019 at 1:37

1 Answer 1

4

You should be able to just add the token value inside your foreach loop:

foreach ($token_values as $token) {
    $request[] = array_merge(array('token' => $token), getCards($merchantId, $merchantKey, $token));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.