Getting data in particular format in PHP for Post request

Viewed 42

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.

2 Answers

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.

posting direct values:

//$altName  = ["domain1.com", "domain2.com"]; 
                //OR 
$altName  = array("domain1.com", "domain2.com");

$postData = ['AltName' => $altName];
var_dump($postData);

output:

array(1) {
  ["AltName"]=>
  array(2) {
    [0]=>
    string(11) "domain1.com"
    [1]=>
    string(11) "domain2.com"
  }
}
Related