0

I am passing the following query string into a PHP method

searchFilterKeyword=ff&searchFilterDateEntered=07%2F29%2F2019&searchFilterStatus=1&searchFilterQuarantineStatus=&searchFilterSize=&searchFilterKennel=&searchFilterType=

How would I explode this so that the following array is generated:

[
  'searchFilterKeyword' => 'ff',
  'searchFilterDateEntered' => '07%2F29%2F2019',
  'searchFilterStatus' => 1,
  'searchFilterQuarantineStatus' => '',
  'searchFilterSize' => '',
  'searchFilterKennel' => '',
  'searchFilterType' => ''
];

Also I would have to reformat the date to remove the %2F. I have to do all of this, because I can't pass in new FormData() object into a jquery call like

{
  'action': 'test',
  'type': 'test',
  'data': new FormData()
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Zach Smith
  • 5,490
  • 26
  • 84
  • 139
  • Your last statement about FormData doesn't seem relevant to the rest of the question. Also note that if that really is the issue then you would simply need to provide ***all*** the values in a FormData object using `append()`, eg. `formData.append('action', 'test')` – Rory McCrossan Aug 13 '19 at 15:03
  • take a look at [`parse_str`](https://www.php.net/manual/en/function.parse-str.php) – Jordi Nebot Aug 13 '19 at 15:06

2 Answers2

3

To covert the query string into an array, you can use parse_str:

$string = 'searchFilterKeyword=ff&searchFilterDateEntered=07%2F29%2F2019&searchFilterStatus=1&searchFilterQuarantineStatus=&searchFilterSize=&searchFilterKennel=&searchFilterType=';
$output = [];
parse_str($string, $output);
print_r($output);

Where $string is your query string and $output is an empty array where the values are populated

Brett Gregson
  • 5,867
  • 3
  • 42
  • 60
2

You can get it done with PHP's parse_str(string, array) method.

This method accepts two parameters. The first one is the string which you want to parse, the other one is the array to store the parsed string.

$arr = [];
$queryStr = 'searchFilterKeyword=ff&searchFilterDateEntered=07%2F29%2F2019&searchFilterStatus=1&searchFilterQuarantineStatus=&searchFilterSize=&searchFilterKennel=&searchFilterType=';
parse_str($queryStr, $arr);
print_r($arr);

Output:

Array
(
    [searchFilterKeyword] => ff
    [searchFilterDateEntered] => 07/29/2019
    [searchFilterStatus] => 1
    [searchFilterQuarantineStatus] => 
    [searchFilterSize] => 
    [searchFilterKennel] => 
    [searchFilterType] => 
)
Kazmi
  • 1,198
  • 9
  • 20