note 103435 added to function.curl-setopt
| From: | serge dot hardy at wanadoo dot fr | Date: | Wed, 13 Apr 2011 13:27:27 +0000 |
| Subject: | note 103435 added to function.curl-setopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178790@lists.php.net to get a copy of this message | ||
in addition to the message from "sam at def dot reyssi dot net" about arrays passed to the
CURLOPT_POSTFIELDS option:
this function will transform any PHP array containing sub-arrays into an array with serialized keys:
<?php
function array_httpize(array $the_array,array $path=array())
{
$res=array();
foreach($the_array as $key=>$value)
{
if(is_scalar($value))
{
$v="";
foreach($path as $p)
{
if(!$v)
$v=$p;
else
$v.="[$p]";
}
$v.="[$key]";
$res[$v]=$value;
}
if(is_array($value))
$res=array_merge( $res,
array_httpize($value,array_merge($path,array($key))));
}
return $res;
}
?>
usage:
<?php
$input=array('request'=>
array('limit'=>10,
'offset'=>"3",
'filters'=>array('id'=>array('1','3')),
'data'=>array('one'=>"foo",'two'=>"bar"),
'file'=>"/path/to/my/file")
);
$output=array_httpize($input);
print_r($output);
/*
Array
(
[request[limit]] => 10
[request[offset]] => 3
[request[filters][id][0]] => 1
[request[filters][id][1]] => 3
[request[data][one]] => foo
[request[data][two]] => bar
[request[file]] => /path/to/my/file
)*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 195.6.79.173
----
Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php
Edit -- https://master.php.net/note/edit/103435
Del: integrated -- https://master.php.net/note/delete/103435/integrated
Del: useless -- https://master.php.net/note/delete/103435/useless
Del: bad code -- https://master.php.net/note/delete/103435/bad+code
Del: spam -- https://master.php.net/note/delete/103435/spam
Del: non-english -- https://master.php.net/note/delete/103435/non-english
Del: in docs -- https://master.php.net/note/delete/103435/in+docs
Del: other reasons-- https://master.php.net/note/delete/103435
Reject -- https://master.php.net/note/reject/103435
Search -- https://master.php.net/manage/user-notes.php