note 43434 deleted from function.http-build-query by aidan
| From: | aidan@php.net | Date: | Mon, 22 Nov 2004 04:22:33 +0000 |
| Subject: | note 43434 deleted from function.http-build-query by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80844@lists.php.net to get a copy of this message | ||
Note Submitter: brooklynphil hotmail com
----
well, i am bored at work so... here is the function for php<5 that DOES work on nested arrays...
;)
<?php
if (!function_exists('http_build_query')){function
http_build_query(&$a,$pref,$f='',$idx='')
{
$ret = '';
foreach ($a as $i => $j)
{
if ($idx != '')
$i = $idx."[$i]";
if (is_array($j))
$ret .= http_build_query($j,'',$f,$i);
else
{
$j=urlencode($j);
if (is_int($i))
$ret .= "$f$pref$i=$j";
else
$ret .= "$f$i=$j";
}
$f='&';
}
return $ret;
}}
$data = array('user'=>array('name'=>'Bob Smith',
'age'=>47,
'sex'=>'M',
'dob'=>'5/12/1956'),
'pastimes'=>array('golf', 'opera', 'poker',
'rap'),
'children'=>array('bobby'=>array('age'=>12,
'sex'=>'M'),
'sally'=>array('age'=>8,
'sex'=>'F')),
'CEO');
echo http_build_query($data, 'flags_');
?>
user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=5%2F12%2F1956&
pastimes[0]=golf&pastimes[1]=opera&pastimes[2]=poker&pastimes[3]=rap&
children[bobby][age]=12&children[bobby][sex]=M&children[sally][age]=8&
children[sally][sex]=F&flags_0=CEO
seems to work the same as the php 5 one above with this example
--Phil
p.s. not sure if it works with objects, but the modification to do so should be trivial and is left
as an exercise to the reader ;p