If a have a form input with a name like "name[0][firstname]", $_POST will retrieve that as:
'name' =>
array (size=1)
0 =>
array (size=9)
'firstname' => null
so echo $_POST['name'][0]['firstname']
will give me null.
The question is how can I build the post query dynamically from the array keys? so, for example:
$foo = array('name', 0, 'firstname');
echo $_POST[$foo]; // obviously doesn't work
What I need do is build this statement, $_POST['name'][0]['firstname']
, from the array but can't find a way to do it. I thought maybe $_POST{'[' . implode('][',$foo) . ']'}
might work but I get Undefined index: ['name'][0]['firstname']
.
Final solution **
With regards to the solution by @Alireza and @Matteo, both work, except in the case if the array is longer than the $_POST array, eg, if array is $foo = array('name', 0, 'firstname', 'lastname') and the post value is array['name'][0]['firstname'] = "XXX" the result will be "X" which is index 0 of the result XXX. Therefore it needs to check the keys of the post and not the value.
The final solution for me is therefore:
function getValue($res, $foo)
{
foreach ($foo as $val) {
if (is_array($res) && array_key_exists($val, $res)) {
$res = $res[$val];
} else {
return false;
}
}
return $res;
}
Examples:
$post = array(
'name' => array(
0 => array(
'firstname' => 'XXX'
),
)
);
echo getValue($post, array('name', 0, 'firstname'));
> XXX
echo getValue($post, array('name', 0, 'firstname', 'lastname'));
> false
echo getValue($post, array('name', 0, 'firstname', 0));
> false
echo getValue($post, array('name', 0, 0));
> false
echo getValue($post, array('name', 0));
> Array (eg, Array ( [firstname] => XXX )) - expected
Thanks for the help.
I think this is very easier :
$foo = array('name', 0, 'firstname')
$res = $_POST;
foreach ($foo as $val) {
$res = $res[$val];
}
echo $res;
$array[0]['f']['p'][8]['Hi'] = 'Hello World!';
$foo = array(0,'f','p',8,'Hi');
$res = $array;
foreach ($foo as $val) {
$res = $res[$val];
}
echo $res;
Echoes 'Hello World!'
Try this out if it works for your use:
function getArrayValue($basearray,$name){
$args = func_get_args();
$argc = func_num_args();
$base = isset($basearray[$name])
? $basearray[$name]
: null;
if($argc > 2 && is_array($base)){
for($i=2; $i < $argc; $i++){
$base = getArrayValue($base,$args[$i]);
}
}
return $base;
}
$myname = getArrayValue($_POST,0,'firstname');
Try this:
function getValue($array, $key) {
$val = NULL;
foreach((array) $key as $k) {
$val = @$array[$k];
$array = @$array[$k];
}
return $val;
}
So if for example
$array = array(
'name' => array(
0 => array(
'firstname' => 'XXX'
),
)
);
then getValue($array, 'name') == array(0 => array('firstname' => 'XXX'))
and getValue($array, array('name', 0, 'firstname')) == 'XXX'
For a live example, see http://sandbox.onlinephpfunctions.com/code/0780e1165ee9851f33fb1e019717ab32cd527d51
First of all, there should be only one closing parenthesis in the following line:
$foo = array('name', 0, 'firstname');
Secondly, you need a recursive function if you would like to create a multidimensional array using the values of $foo
as keys:
function create_multidimensional_array(&$arr, $keys, $val){
if($keys === array()){
$arr = $val;
}
else{
$key = array_shift($keys);
$arr[$key] = array();
create_multidimensional_array($arr[$key], $keys, $val);
}
}
$foo = array('name', 0, 'firstname');
create_multidimensional_array($_POST, $foo, "Charles");
print_r($_POST);
The output will be:
Array
(
[name] => Array
(
[0] => Array
(
[firstname] => Charles
)
)
)