PHP Function to Build Query String from Array

PHP function to build query string from array

You're looking for http_build_query().

How to construct a PHP array querystring

You can use http_build_query:

$merchantFilter= Array ( 'Diesel Monkeys', 'Gas Monkeys' );
echo http_build_query(array('merchantFilter' => $merchantFilter));

Output:

merchantFilter%5B0%5D=Diesel+Monkeys&merchantFilter%5B1%5D=Gas+Monkeys

Note the result is already urlencoded so needs no further processing.

Build query string with indexed array data

This can be achieved with the http-build-query method itself of php. The second param accepts the prefix to prepend to the key.
The key should be of numeric type

You can use the http-build-query like this in your case

http_build_query($field_data, 'question');

PHP function to build query string from array - not http build query

I don't know of anything offhand. What I'd recommend is first iterating through the array and covert booleans to strings, then call http_build_query

E.g.

foreach($options_array as $key=>$value) :
if(is_bool($value) ){
$options_array[$key] = ($value) ? 'true' : 'false';
}
endforeach;
$options_string=http_build_query($options_array);

Parse PHP Array To Build Query String

Below is the working PHP code. Instead of checking each, you can use implode().

<?php
if (!isset($_POST['employee_data'])) die();
$query = "Select employeename ";
$query .= implode(', ', $_POST['employee_data']);
$query .= " FROM employeepersonelinfo where employeename = '" .
$_POST['employeename'] . "'";
echo $query;
?>

You don't really need to check each and every value. You can save lines of code and lines of conditions by using array functions.

Generate URL with parameters from an array

All you need is http_build_query:

$final = $url . "?" . http_build_query($subids);

How to serialize an array (array to query string) using PHP

You can use http_build_query:

curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query($this->_postFields));

NOTICE

It looks beauty, but to use this approach be careful about url encoding .Look next:

$_POST["some value"]='value1'; // " " between
$_POST["next value"]='value2'; // " " between

$url = http_build_query($_POST);

echo $url;

// OUTPUT
some+value=value1&next+value=value2

Of course, after sending this $url we will not get expected variables from $_POST.

How to pass an array within a query string?

Here's what I figured out:

Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.

Three possible ways to send multi-value fields or arrays would be:

  • ?cars[]=Saab&cars[]=Audi (Best way- PHP reads this into an array)
  • ?cars=Saab&cars=Audi (Bad way- PHP will only register last value)
  • ?cars=Saab,Audi (Haven't tried this)

Form Examples

On a form, multi-valued fields could take the form of a select box set to multiple:

<form> 
<select multiple="multiple" name="cars[]">
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</option>
</select>
</form>

(NOTE: In this case, it would be important to name the select control some_name[], so that the resulting request vars would be registered as an array by PHP)

... or as multiple hidden fields with the same name:

<input type="hidden" name="cars[]" value="Volvo">
<input type="hidden" name="cars[]" value="Saab">
<input type="hidden" name="cars[]" value="Mercedes">

NOTE: Using field[] for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.


UPDATE

As commenters have pointed out, this is very much framework-specific. Some examples:

Query string:

?list_a=1&list_a=2&list_a=3&list_b[]=1&list_b[]=2&list_b[]=3&list_c=1,2,3

Rails:

"list_a": "3", 
"list_b":[
"1",
"2",
"3"
],
"list_c": "1,2,3"

Angular:

 "list_a": [
"1",
"2",
"3"
],
"list_b[]": [
"1",
"2",
"3"
],
"list_c": "1,2,3"

(Angular discussion)

See comments for examples in node.js, Wordpress, ASP.net


Maintaining order:
One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.

Is there a PHP function to convert a query string to an array?

You want parse_str(). Pass it an array as the 2nd parameter and it will extract variables from the query string you give it into the array:

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";

parse_str($str, $output);

print_r($output);

/*
Array
(
[first] => value
[arr] => Array
(
[0] => foo bar
[1] => baz
)

)
*/

Notice this is the very first related function listed on the http_build_query page.

Parse query string into an array

You want the parse_str function, and you need to set the second parameter to have the data put in an array instead of into individual variables.

$get_string = "pg_id=2&parent_id=2&document&video";

parse_str($get_string, $get_array);

print_r($get_array);


Related Topics



Leave a reply



Submit