note 111072 deleted from reserved.variables.post by aharvey
| From: | aharvey@php.net | Date: | Mon, 14 Jan 2013 01:46:54 +0000 |
| Subject: | note 111072 deleted from reserved.variables.post by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193018@lists.php.net to get a copy of this message | ||
Note Submitter: ukasz Gieratowski
----
If we have form Like that:
<form>
<input name="bob" value="1">
<input name="ala[ola][ela]" value="2">
</form>
And We will send the form, we will get multidimensional array:
$_POST = Array(
[bob] => 1
[ala] => Array(
[ola] => Array(
[ela]=>2
)
)
)
But when we have only array and want to make form, there is usefull function:
<?php
function extractPost($array, $parent_key=null){
$ret = array();
foreach($array as $key=>$val){
if(is_array($val)){
$children = extractPost($array[$key], $parent_key.$key.'][');
$ret = array_merge($ret, $children);
} else {
$ret[$parent_key.$key]=$val;
}
}
return $ret;
}
?>
Example:
<?php
$array = array('bob'=>1,
'ala'=>array('ola'=>array('ela'=>2)));
print_r($array);
print_r(extractPost($array));
?>
Output:
Array(
[bob] => 1
[ala] => Array(
[ola] => Array(
[ela] => 2
)
)
)
Array(
[bob] => 1
[ala][ola][ela] => 2
)