简体   繁体   English

PHP插入关联数组

[英]PHP insert associative array

I would need to insert an associative array into my existing code, to make an HTTP request. 我需要在现有代码中插入一个关联数组,以发出HTTP请求。

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? 现在如何确保$ payload数组的每个元素都插入到form_params数组中?

I tried using: 我尝试使用:

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

But I don't know how to proceed inside the form_params element? 但是我不知道如何在form_params元素内进行操作?

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
    ]

You should just be able to just use the $payload variable directly, like so: 您应该只能直接使用$payload变量,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM