如何使用字符串从多维数组中获取值?

时间:2023-02-05 19:44:51

Say this is my string

说这是我的字符串

$string = 'product[0][1][0]';

How could I use that string alone to actually get the value from an array as if I had used this:

我怎么能单独使用该字符串来实际从数组中获取值,就像我使用过这个:

echo $array['product'][0][1][0]

I've messed around with preg_match_all with this regex (/\[([0-9]+)\]/), but I am unable to come up with something satisfactory.

我用这个正则表达式(/ \ [[[0-9] +)\] /)弄乱了preg_match_all,但是我无法想出令人满意的东西。

Any ideas? Thanks in advance.

有任何想法吗?提前致谢。

3 个解决方案

#1


You could use preg_split to get the individual array indices, then a loop to apply those indices one by one. Here's an example using a crude /[][]+/ regex to split the string up wherever it finds one or more square brackets.

您可以使用preg_split来获取单个数组索引,然后使用循环逐个应用这些索引。这是一个使用原始/ [] [] + /正则表达式将字符串拆分到找到一个或多个方括号的示例。

(Read the [][] construct as [\]\[], i.e. a character class that matches right or left square brackets. The backslashes are optional.)

(将[] []构造读作[\] \ [],即与右方括号或左方括号匹配的字符类。反斜杠是可选的。)

function getvalue($array, $string)
{
    $indices = preg_split('/[][]+/', $string, -1, PREG_SPLIT_NO_EMPTY);

    foreach ($indices as $index) 
        $array = $array[$index];

    return $array;
}

#2


This is prettttty hacky, but this will work. Don't know how much your array structure is going to change, either, this won't work if you get too dynamic.

这是非常好的hacky,但这会奏效。不知道你的数组结构会改变多少,如果你太动态了,这将不起作用。

$array = array();

$array['product'][0][1][0] = "lol";

$string = 'product[0][1][0]';

$firstBrace = strpos( $string, "[" );

$arrayExp = substr($string, $firstBrace );
$key = substr( $string, 0, $firstBrace );

echo $arrayExp, "<br>";
echo $key, "<br>";

$exec = "\$val = \$array['".$key."']".$arrayExp.";";

eval($exec);

echo $val;

#3


What about using eval()?

那么使用eval()呢?

<?php
  $product[0][1][0] = "test";
  eval ("\$string = \$product[0][1][0];");
  echo $string . "\n";
  die();
?>

#1


You could use preg_split to get the individual array indices, then a loop to apply those indices one by one. Here's an example using a crude /[][]+/ regex to split the string up wherever it finds one or more square brackets.

您可以使用preg_split来获取单个数组索引,然后使用循环逐个应用这些索引。这是一个使用原始/ [] [] + /正则表达式将字符串拆分到找到一个或多个方括号的示例。

(Read the [][] construct as [\]\[], i.e. a character class that matches right or left square brackets. The backslashes are optional.)

(将[] []构造读作[\] \ [],即与右方括号或左方括号匹配的字符类。反斜杠是可选的。)

function getvalue($array, $string)
{
    $indices = preg_split('/[][]+/', $string, -1, PREG_SPLIT_NO_EMPTY);

    foreach ($indices as $index) 
        $array = $array[$index];

    return $array;
}

#2


This is prettttty hacky, but this will work. Don't know how much your array structure is going to change, either, this won't work if you get too dynamic.

这是非常好的hacky,但这会奏效。不知道你的数组结构会改变多少,如果你太动态了,这将不起作用。

$array = array();

$array['product'][0][1][0] = "lol";

$string = 'product[0][1][0]';

$firstBrace = strpos( $string, "[" );

$arrayExp = substr($string, $firstBrace );
$key = substr( $string, 0, $firstBrace );

echo $arrayExp, "<br>";
echo $key, "<br>";

$exec = "\$val = \$array['".$key."']".$arrayExp.";";

eval($exec);

echo $val;

#3


What about using eval()?

那么使用eval()呢?

<?php
  $product[0][1][0] = "test";
  eval ("\$string = \$product[0][1][0];");
  echo $string . "\n";
  die();
?>