PHP 中变量的数组路径

发布于 2024-11-04 05:33:21 字数 750 浏览 8 评论 0原文

因此,我编写了一个类,它可以解析 XML 文档并从中创建 SQL 查询,以根据设置更新或插入新行。

由于脚本必须处理任意数量的嵌套块,因此我放入所有值的数组的路径是动态创建的,与以下示例非常相似:

$path = array('field1','field2');
$path = "['".implode("']['",$path)."']";

eval("\$array".$path."['value'] = 'test';");

基本上 $path 包含一个显示的数组如果 $path 包含例如值 main_tablefield 我想设置 $array[ 'main_table']['field']['value']'test'

如您所见,我目前正在使用 eval 来执行此操作,并且效果很好。我只是想知道是否有一种方法可以在不使用 eval 的情况下做到这一点。

像这样的东西 $array{$path}['value'] = 'test'; 但实际上是有效的。

有什么建议吗?

编辑

我正在寻找替代方案的原因是因为我认为 eval 是不好的做法。

第二次编辑

将实际代码更改为虚拟代码,因为它引起了很多误解。

So I wrote a class that can parse XML documents and create SQL queries from it to update or insert new rows depending on the settings.

Because the script has to work with any amount of nested blocks, the array that I'm putting all the values in has its path dynamically created much like the following example:

$path = array('field1','field2');
$path = "['".implode("']['",$path)."']";

eval("\$array".$path."['value'] = 'test';");

Basically $path contains an array that shows how deep in the array we currently are, if $path contains for instance the values main_table and field I want set $array['main_table']['field']['value'] to 'test'

As you can see I am currently using eval to do this, and this works fine. I am just wondering if there is a way to do this without using eval.

Something like
$array{$path}['value'] = 'test'; but then something that actually works.

Any suggestions?

EDIT

The reason I'm looking for an alternative is because I think eval is bad practice.

SECOND EDIT

Changed actual code to dummy code because it was causing a lot of misunderstandings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烂柯人 2024-11-11 05:33:21

使用这样的东西:

/**
 * Sets an element of a multidimensional array from an array containing
 * the keys for each dimension.
 * 
 * @param array &$array The array to manipulate
 * @param array $path An array containing keys for each dimension
 * @param mixed $value The value that is assigned to the element
 */
function set_recursive(&$array, $path, $value)
{
  $key = array_shift($path);
  if (empty($path)) {
    $array[$key] = $value;
  } else {
    if (!isset($array[$key]) || !is_array($array[$key])) {
      $array[$key] = array();
    }
    set_recursive($array[$key], $path, $value);
  }
}

Use something like this:

/**
 * Sets an element of a multidimensional array from an array containing
 * the keys for each dimension.
 * 
 * @param array &$array The array to manipulate
 * @param array $path An array containing keys for each dimension
 * @param mixed $value The value that is assigned to the element
 */
function set_recursive(&$array, $path, $value)
{
  $key = array_shift($path);
  if (empty($path)) {
    $array[$key] = $value;
  } else {
    if (!isset($array[$key]) || !is_array($array[$key])) {
      $array[$key] = array();
    }
    set_recursive($array[$key], $path, $value);
  }
}
暗恋未遂 2024-11-11 05:33:21

您可以使用数组追加运算符绕过整个计数器业务:

$some_array[] = 1; // pushes '1' onto the end of the array

至于整个路径业务,我假设这基本上是通过您的 xml 文档的类似 xpath 的路线的奇怪表示...您不能简单地使用的任何原因该字符串本身作为数组键?

$this->BLOCKS['/path/to/the/node/you're/working/on][] = array('name' => $name, 'target' => $target);

You can bypass the whole counter business with the array append operator:

$some_array[] = 1; // pushes '1' onto the end of the array

As for the whole path business, I'm assuming that's basically an oddball representation of an xpath-like route through your xml document... any reason you can't simply use that string as an array key itself?

$this->BLOCKS['/path/to/the/node/you're/working/on][] = array('name' => $name, 'target' => $target);
倾城°AllureLove 2024-11-11 05:33:21

您可以将 foreach 与 变量 结合使用。

// assuming a base called $array, and the path as in your example:
$path = array('field1','field2');

$path = $array;
foreach ($path as $v) $path = $path[$v];
$path['value'] = 'test';

简短、简单,并且比 eval 好得多。

You can use a foreach with variable variables.

// assuming a base called $array, and the path as in your example:
$path = array('field1','field2');

$path = $array;
foreach ($path as $v) $path = $path[$v];
$path['value'] = 'test';

Short, simple, and much better than eval.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文