PHP Send Duplicate Key in POST data

Some Application Programming Interface (API) requires you to send multiple parameter with same key name. However, associative array in PHP only allow unique key name per each array. Subsequent assignment towards the same key will override the previously assigned values. Here is how to send duplicate key in POST data with PHP.

Example scenario:

  • status=paid
  • status=refunded
  • status=settled
  • status=cleared

If we try to define it with PHP Associative Array, the only value will be the last assigned, which is cleared.

So, this is how it should be defined:

$parameter = [
  'status' => ['paid', 'refunded', 'settled', 'cleared']
];

Later, you will need to use this code to generate the build query and it will produce this.

status=paid&status=refunded&status=settled&status=cleared

If you found this article useful, you may upvote my answer in stack overflow.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *