Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting data in particular format in PHP for Post request

I have to send some data in the below format-

$postData = [
            'certExpiryDate' => $certExpiryDate,
            'certType' => 'SSL',
            'AltName' => [
                $altName[0],$altName[1]
            ],
            'csr' => $csr
        ];

$altName is an array that has 2 or more strings like- domain1.com, domain2.com.

The AltName parameter expects the data in this format-

'AltName' => [
    'domain1.com','domain2.com' 
],

(if I hardcode and send it like this, everything works fine.. this is the correct format)

If I take the array $altName directly like this-

'AltName' => [
        $altName 
    ],

I did a var_dump and for this it adds quotes outside like- "domain1.com, domain2.com", which it considers wrong.

I need it to be in the format of $altName[0],$altName[1] which would be correct, but I'm struggling on how to loop through this array in case it has more values than 2 and still get it in the required format.

like image 536
manishk Avatar asked Jan 26 '26 11:01

manishk


1 Answers

Your attempt:

'AltName' => [
    $altName 
 ],

doesn't work as intended because it wraps the array $altName inside another array (created by the [ and ]).

Just set the $altName array to be the value of the property directly. There's no need for an extra array:

'AltName' => $altName

Demo: http://sandbox.onlinephpfunctions.com/code/fa3dd6a974be7ced4bdc5d878f692549b96e2b95

like image 153
ADyson Avatar answered Jan 29 '26 03:01

ADyson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!