I am using arrays in PHP to modify xml data and write it back. This is the xml structure (simplified for demonstration purpose):

我在PHP中使用数组来修改xml数据并将其写回。这是xml结构(为了演示目的而简化):

<docs>
  <folder>
    <name>Folder name</name>
    <date>20.06.2009</date>
    <folder>
      <name>Subfolder1</name>
      <date></date>
    </folder>
    <folder>
      <name>Subfolder1</name>
      <date></date>
    </folder>
    <file>
      <name></name>
    </file>
  </folder>
  <name></name>
  <date></date>
</docs>

Using this code, this is then parsed and transformed into a multidimensional array:

使用此代码,然后将其解析并转换为多维数组:

Array
(
    [docs] => Array
        (
            [_c] => Array
                (
                    [folder] => Array
                        (
                            [_c] => Array
                                (
                                    [name] => Array
                                        (
                                            [_v] => Folder name
                                        )

                                    [date] => Array
                                        (
                                            [_v] => 20.06.2009
                                        )

                                    [folder] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [_c] => Array
                                                        (
                                                            [name] => Array
                                                                (
                                                                    [_v] => Subfolder1
                                                                )

                                                            [date] => Array
                                                                (
                                                                    [_v] => 
                                                                )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [_c] => Array
                                                        (
                                                            [name] => Array
                                                                (
                                                                    [_v] => Subfolder1
                                                                )

                                                            [date] => Array
                                                                (
                                                                    [_v] => 
                                                                )

                                                        )

                                                )

                                        )

                                    [file] => Array
                                        (
                                            [_c] => Array
                                                (
                                                    [name] => Array
                                                        (
                                                            [_v] => 
                                                        )

                                                )

                                        )

                                )

                        )

                    [name] => Array
                        (
                            [_v] => 
                        )

                    [date] => Array
                        (
                            [_v] => 
                        )

                )

        )

)

Lengthy, I know. But now to the actual issue. If I want to add another file to a sub folder called Subfolder2 in this case, it's not a problem to do it by hand when u see the structure:

我知道,很冗长但现在到了实际问题。如果我想在这种情况下将另一个文件添加到名为Subfolder2的子文件夹中,当您看到结构时,手动执行它不是问题:

array_push($array['docs']['_c']['folder']['_c']['folder'][1], $newfile);

Now when I want to do it via the function that only knows a path to the folder (like docs/Folder name/Subfolder2), the algorithm has to analyze the array structure (check the name of each [folder], check if there is one or more folders ['_c'] or [number]) - all good, but I cannot find a way to create a variable that would have an "array" path for that new file.

现在当我想通过只知道文件夹路径的函数(如docs / Folder name / Subfolder2)来做这个时,算法必须分析数组结构(检查每个[文件夹]的名称,检查是否有一个或多个文件夹['_c']或[数字]) - 一切都很好,但我找不到一种方法来创建一个具有该新文件的“数组”路径的变量。

I was thinking somewhere along these lines:

我正在考虑这些方面:

$writepath = "['docs']['_c']['folder']...[1]"; // path string
array_push($array{$writepath}, $newfile);

Of course this is not a valid syntax.

当然这不是一个有效的语法。

So, how can I make a variable that contains a path through the array elements? I did a bit of research on w3c and php.net finding no helpful info on multidimensional arrays...

那么,我如何创建一个包含数组元素路径的变量呢?我对w3c和php.net做了一些研究,发现没有关于多维数组的有用信息......

If anyone has any other suggestions regarding structure, xml transformation/manipulation etc. by all means, I know it is far from sufficient way of data handling.

如果有人对结构,xml转换/操作等有任何其他建议,我知道它远远没有足够的数据处理方式。

Thanks for any input,

感谢您的任何意见,

Erik


Edit: Regarding the reference, is it possible to reference the reference? As that would be the way to move the 'pointer' through a set of arrays? Something as such:

编辑:关于参考,是否可以参考参考?那会是通过一组数组移动'指针'的方法吗?这样的东西:

$pointer = &$array['docs'];
if (key($pointer) == '_c') { $pointer = &$pointer['_c']; }
else (
  // create an array with '_c' key instead of empty '_v' array
)

This syntax does not work.

此语法不起作用。

Edit: The syntax works, never mind... Thanks for all your help guys!

编辑:语法有效,没关系...感谢您的帮助!

2 个解决方案

#1


Although this isn't exactly an answer to your question: Instead of the xml<->array code you could use SimpleXML and its XPath capabilities.

虽然这不是您的问题的答案:您可以使用SimpleXML及其XPath功能代替xml < - >数组代码。

<?php
$xml = '<docs>
  <folder>
    <name>Folder name</name>
    <date>20.06.2009</date>
    <folder>
      <name>Subfolder1</name>
      <date></date>
    </folder>
    <folder>
      <name>Subfolder2</name>
      <date></date>
      <folder>
        <name>Subfolder3</name>
        <date></date>
      </folder>
    </folder>
  </folder>
</docs>';

$doc = new SimpleXMLElement($xml); $pathToParentelement = '//folder[name="Subfolder3"]'; $element = $doc->xpath($pathToParentelement); isset($element[0]) or die('not found');
$newFolder = $element[0]->addChild("folder"); $newFolder->name = "Subfolder4.1"; $newFolder->date = date(DATE_RFC822);
// let's see the result echo $doc->asxml();

$ doc = new SimpleXMLElement($ xml); $ pathToParentelement ='// folder [name =“Subfolder3”]'; $ element = $ doc-> xpath($ pathToParentelement); isset($ element [0])或die('not found'); $ newFolder = $ element [0] - > addChild(“folder”); $ newFolder-> name =“Subfolder4.1”; $ newFolder-> date = date(DATE_RFC822); //让我们看看结果echo $ doc-> asxml();

$pathToParentelement is more or less your $writepath.

更多相关文章

  1. 键入提示 - 指定对象数组
  2. 切出多维数组的一部分
  3. 仅获取单元素php数组中的值的最佳方法。
  4. 如何从其他文件调用数组
  5. php如何判断数组["a","b","c"]中是否含有"a"?
  6. 如何从databse中提取数据到2d数组中?
  7. 如果key在变量中,PHP如何从数组中获取值
  8. 是否可以在PHP中合并这些数组? [重复]
  9. PHP打印输出数组内容及结构函数print_r与var_dump

随机推荐

  1. MySQL:讨人喜欢的 MySQL replace into 用
  2. [新开一贴]php5.4 连接Sqlserver2008 的
  3. mysqldump的几个主要选项探究
  4. MYSQL学习笔记(三)
  5. mysql在组通过之前获得顺序
  6. mysql数据备份恢复和导入
  7. 如何将frm格式MYD格式MYI格式文件导入MyS
  8. 在C#.net的server explorer 上建立的数据
  9. 有人用过查询分析器里的TSQL调试程序吗?
  10. MySQL锁机制及优化