I want to dynamically create a short array with a list of objects. This is for a POST request with the Guzzle client. That's why I need it in a short array.
example of a Guzzle post request:
$res = $this->client->request($methode, $request_url, [
'form_params' => [
'param' => 'value'
]
]);
Problem case:
I have got a List: Params of Objects: Param. Param has three attributes id, name, link_id.
Let's say the List has three Object.
param(1, email, 1)
param(2, username, 1)
param(3, password, 1)
I want to dynamically create from the list an array with the short array syntax.
Example(Pseudo):
for each params as param
[
'form_params' => [
param->name => 'value'
]
]
the result of this code will be like this
[
'form_params' => [
'email' => 'value',
'username' => 'value',
'password' => 'value'
]
]
Code example:
$params = array(
"param" => array (
"id" => "1",
"name" => "username",
"link_id" => "1",
)
);
$value = '';
$shortarray = '';
foreach($params as $key => $param){
$shortarray .= $param->name . '=>' . $value . ',';
}
$postParams = ['form_params' => [ . $shortarray . ]];
I really could use some help. Thank you in advance.