php


PHP - multidimensional array to query string


Searching for so long but didn't got any feasible answer.
A) Input:
$array = array(
'order_source' => array('google','facebook'),
'order_medium' => 'google-text'
);
Which looks like:
Array
(
[order_source] => Array
(
[0] => google
[1] => facebook
)
[order_medium] => google-text
)
B) Required output:
order_source=google&order_source=facebook&order_medium=google-text
C) What I've tried (http://3v4l.org/b3OYo):
$arr = array('order_source' => array('google','facebook'), 'order_medium' => 'google-text');
function bqs($array, $qs='')
{
foreach($array as $par => $val)
{
if(is_array($val))
{
bqs($val, $qs);
}
else
{
$qs .= $par.'='.$val.'&';
}
}
return $qs;
}
echo $qss = bqs($arr);
D) What I'm getting:
order_medium=google-text&
Note: It should also work for any single dimensional array like http_build_query() works.
I hope that this is what you are looking for, it works with single to n-dimensinal arrays.
$walk = function( $item, $key, $parent_key = '' ) use ( &$output, &$walk ) {
is_array( $item )
? array_walk( $item, $walk, $key )
: $output[] = http_build_query( array( $parent_key ?: $key => $item ) );
};
array_walk( $array, $walk );
echo implode( '&', $output ); // order_source=google&order_source=facebook&order_medium=google-text
Although as you found in the comments if you are trying to pass this as $_GET you will have override problems, the solution to your problem to get desired results using recursive functions would be:
function bqs($array, $qs='',$index = false)
{
foreach($array as $par => $val)
{
if($index)
$par = $index;
if(is_array($val))
{
$qs = bqs($val, $qs,$par);
}
else
{
$qs .= $par.'='.$val.'&';
}
}
return $qs;
}
where i am concatinating the $qs string if it's an array and passing the index as a reference along with the value if it's an array()
fixed
After supplying the $index you do not need to concatenate again. See here: http://3v4l.org/QHF5G
You don't really need to do anything special here.
$array = array(
'order_source' => array('google', 'facebook'),
'order_medium' => 'google-text'
);
$qss = http_build_query($array);
On the other side:
var_dump($_GET);
Result:
array(2) {
["order_source"]=>
array(2) {
[0]=>
string(6) "google"
[1]=>
string(8) "facebook"
}
["order_medium"]=>
string(11) "google-text"
}
This really is the best way to send arrays as GET variables.
If you absolutely must have the output as you've defined, this will do it:
function bqs($array, $qs = false) {
$parts = array();
if ($qs) {
$parts[] = $qs;
}
foreach ($array as $key => $value) {
if (is_array($value)) {
foreach ($value as $value2) {
$parts[] = http_build_query(array($key => $value2));
}
} else {
$parts[] = http_build_query(array($key => $value));
}
}
return join('&', $parts);
}

Related Links

How to send request to Graph Api and fetch events data?
How to explode string using capital letters as delimiter?
Get realtime photo updates of a tag from instagram
Suitable Php Framework For Shopping Cart Website
show result of date_diff in seconds
Formatting a date in Codeigniter using MySQL
Regex/PHP quandry
Using Klein-PHP to filter requests
PHP cURL is holding onto session cookies
Open an accordion item from an anchor tag in an external page
Error message and check the fields
Integrating DB info from inside of an html table to output a single row's info to another page
Get no data from NSURLRequest (asynchronous) from php/json service
Compress and upload to AWS S3
Trying to strip li tags from get_archives_link
Page listing post_type pagination not working

Categories

HOME
typelite
verification
schema.org
paypal-ipn
long-integer
swi-prolog
ews
video-streaming
native-base
yahoo
solution
live-streaming
pushdown-automaton
spring-jpa
xbox360
async-await
temperature
google-tasks-api
exec-maven-plugin
cpu-usage
psql
android-custom-view
batch-rename
mime-types
device
backendless
restier
web-push
background-color
android-service
tf-idf
acrobat
bobo-browse.net
spring-annotations
laravel-4.2
guice
nic
xenapp
cloudera-manager
image-optimization
custom-keyboard
amazon-kms
dreamfactory
testlink
love2d
jade4j
nanogallery
django-crispy-forms
cloudera-quickstart-vm
telephonymanager
gitolite
oauth2
stat
pyttsx
jshint
xcode8.2
.net-micro-framework
deployd
embedded-v8
skip-lists
alertify
perl-data-structures
ternary-operator
elastic4s
jsf-2.3
svnserve
nvda
zynq
always-on-top
callouts
mathml
fragmentstatepageradapter
declarative
management-studio-express
voronoi
miniconda
srv-record
enaml
uitest
r.java-file
tidyr
fat32
datagridcomboboxcolumn
nidaqmx
network-printers
.htpasswd
oxygene
nosql-aggregation
jmenubar
stty
google-code-prettify
drawimage
asp.net-mvc-views
gaelyk
android-2.1-eclair
sscli
cleartype
resharper-5.0
browser-based
source-code-protection
tso
rakudo
html-generation
word-automation

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile