如何使用字符串在PHP中获取关联HTML数组的值?

时间:2022-10-27 10:55:21

Look I have a form like this:

看,我有一个这样的表格:

<form method="post">
    <input name="variable[var1][var2]" value="44"/>
</form>

I want to get the value of variable[var1][var2] in PHP using a string like:

我想在PHP中使用如下字符串获取变量[var1] [var2]的值:

$str = "['variable']['var1']['var2']";
echo "{$_POST{$str}}";

Why I need to do that?

为什么我需要这样做?

I need it because the code that gets the value is totally dynamic and I cannot get or set manually the value using $_POST['variable']['var1']['var2'].

我需要它,因为获取值的代码是完全动态的,我无法使用$ _POST ['variable'] ['var1'] ['var2']手动获取或设置值。

Can you help?

你能帮我吗?

NOTE: I know this is possible to get it using $_POST['variable']['var1']['var2'], please don't ask me about why I'm not using this way. Simply I need to use as above (using $str).

注意:我知道可以使用$ _POST ['variable'] ['var1'] ['var2']来获取它,请不要问我为什么我不使用这种方式。我只需要使用如上所述(使用$ str)。

3 个解决方案

#1


2  

You can use preg_match_all() to get all the indexes in the string into an array:

您可以使用preg_match_all()将字符串中的所有索引转换为数组:

$indexes = preg_match_all('/(?<=\[\')(?:[^\']*)(?=\'\])/', $str);
$indexes = $indexes[0]; // Get just the whole matches

Then use a loop to drill into $_POST.

然后使用循环钻取$ _POST。

$cur = $_POST;
foreach ($indexes as $index) {
    $cur = $cur[$index];
}

At this point $cur will contain the value you want.

此时$ cur将包含您想要的值。

#2


0  

You're WAY overthinking it. Anything you put in [] array notation in a form field's name will just become an array key. The following is LITERALLY all you need:

你想要过度思考它。您在表单字段名称中放入[]数组表示法的任何内容都将成为数组键。以下是您所需要的所有内容:

<input name="foo[bar][baz][qux]" ... />

$val = $_POST['foo']['bar']['baz']['qux'];

You cannot use a string as an "address" into the array, not without insanely ugly hacks like eval, or parsing the string and doing a loop to dig into the array.

你不能将字符串用作数组中的“地址”,不是没有像eval那样疯狂的丑陋黑客,也不能解析字符串并进行循环挖掘数组。

#3


-1  

It's hard to believe that this is a requirement. If you could expand more on what you're trying to achieve, someone undoubtedly has a better solution. However, I will ignore the eval = evil haters.

很难相信这是一个要求。如果你可以进一步扩展你想要实现的目标,那么毫无疑问,有人会有更好的解决方案。但是,我会忽略eval =邪恶的仇敌。

To echo:

回应:

eval("echo \$_POST$str;");

To assign to a variable:

要分配给变量:

eval("\$result = \$_POST$str;");

If you're open to another syntax then check How to write getter/setter to access multi-level array by key names?

如果您对另一种语法开放,那么请检查如何编写getter / setter以按键名访问多级数组?

#1


2  

You can use preg_match_all() to get all the indexes in the string into an array:

您可以使用preg_match_all()将字符串中的所有索引转换为数组:

$indexes = preg_match_all('/(?<=\[\')(?:[^\']*)(?=\'\])/', $str);
$indexes = $indexes[0]; // Get just the whole matches

Then use a loop to drill into $_POST.

然后使用循环钻取$ _POST。

$cur = $_POST;
foreach ($indexes as $index) {
    $cur = $cur[$index];
}

At this point $cur will contain the value you want.

此时$ cur将包含您想要的值。

#2


0  

You're WAY overthinking it. Anything you put in [] array notation in a form field's name will just become an array key. The following is LITERALLY all you need:

你想要过度思考它。您在表单字段名称中放入[]数组表示法的任何内容都将成为数组键。以下是您所需要的所有内容:

<input name="foo[bar][baz][qux]" ... />

$val = $_POST['foo']['bar']['baz']['qux'];

You cannot use a string as an "address" into the array, not without insanely ugly hacks like eval, or parsing the string and doing a loop to dig into the array.

你不能将字符串用作数组中的“地址”,不是没有像eval那样疯狂的丑陋黑客,也不能解析字符串并进行循环挖掘数组。

#3


-1  

It's hard to believe that this is a requirement. If you could expand more on what you're trying to achieve, someone undoubtedly has a better solution. However, I will ignore the eval = evil haters.

很难相信这是一个要求。如果你可以进一步扩展你想要实现的目标,那么毫无疑问,有人会有更好的解决方案。但是,我会忽略eval =邪恶的仇敌。

To echo:

回应:

eval("echo \$_POST$str;");

To assign to a variable:

要分配给变量:

eval("\$result = \$_POST$str;");

If you're open to another syntax then check How to write getter/setter to access multi-level array by key names?

如果您对另一种语法开放,那么请检查如何编写getter / setter以按键名访问多级数组?