note 43434 added to function.http-build-query
| From: | brooklynphilhotmailcom at rn2 dot php dot net | Date: | Mon, 21 Jun 2004 16:08:36 +0000 |
| Subject: | note 43434 added to function.http-build-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-71445@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.http-build-query.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43434
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43434&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43434&report=yes
Search -- http://master.php.net/manage/user-notes.php