john_doee
john_doee

Reputation: 45

PHP insert associative array

I would need to insert an associative array into my existing code, to make an HTTP request.

My code at the moment:

$payload =[
    'method_id' => 2,
    'api_key'   => 5,
];
$res = $client->post('some.website', [,
    'form_params' => [
        foreach($this->payload as $key => $s_key) {
            $key => $s_key;
        }
    ],
]);

How to make now sure, each element of the $payload array is inserted into the form_params array?

I tried using:

foreach ($this->payload as $s_key => $key) {
    //?!
}

But I don't know how to proceed inside the form_params element?

Using the payload array directly inside form elements is resulting into this:

    "form_params" => [
        0 => array:2 [
          "method_id" => 2
          "api_key" => 5
        ]
    ]

What I would need is something like this:

    "form_params" => [
          "method_id" => 2
          "api_key" => 5
    ]

Upvotes: 0

Views: 65

Answers (1)

cryptichorizon
cryptichorizon

Reputation: 36

You should just be able to just use the $payload variable directly, like so:

$res = $client->post('some.website', [
    'form_params' => $payload,
]);

Upvotes: 1

Related Questions