2

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.

4
  • 1
    try var_dump( $_POST ); to see what you're getting. Commented Jan 27, 2014 at 13:00
  • echo $_POST[$foo[0]][$foo[1]][$foo[2]]; maybe? Commented Jan 27, 2014 at 13:01
  • Bit vague & crazy. But $_POST[$foo[0]][$foo[1]][$foo[2]]; is that what you want ? Commented Jan 27, 2014 at 13:01
  • Thanks everyone, @Alireza answered the question below Commented Jan 27, 2014 at 20:35

4 Answers 4

1

I think this is very easier :

$foo = array('name', 0, 'firstname') $res = $_POST; foreach ($foo as $val) { $res = $res[$val]; } echo $res; 

Example :

$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!'

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! That's exactly what I wanted. Thank you.
1

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'); 

Comments

1

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 ) ) ) 

Comments

0

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

6 Comments

Thank @Matteo, that works but I needed to create a post statement
What does that mean? My function works perfectly using $_POST as the array: getValue($_POST, array('name', 0, 'firstname'));
Actually that only works for finding a value of a key in an array but I was more after something like getValue($_POST, 'name[0]['firstname'])) which would ultimately result in $val = $_POST['name'][0]['firstname'].
The point is I need to dynamically create the post variable from an array. In my script i have the name of the field stored as 'name[0]['firstname'] which is easily to convert to ['name'][0]['firstname']. I just did not know how to combine it with $_POST to make $var = $_POST['name'][0]['firstname'];
I still do not understand, but it doesn't matter.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.