note 103435 deleted from function.curl-setopt by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 Apr 2011 13:43:29 +0000 |
| Subject: | note 103435 deleted from function.curl-setopt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178792@lists.php.net to get a copy of this message | ||
Note Submitter: serge dot hardy at wanadoo dot fr
----
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
)*/
?>