Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
合并PHP数组和XML对象_Php_Xml_Arrays - Fatal编程技术网

合并PHP数组和XML对象

合并PHP数组和XML对象,php,xml,arrays,Php,Xml,Arrays,我有一个这样的数组,我通过SQL查询得到它 Array ( [0] => Array ( [iId] => 1 [sName] => Tom ) [1] => Array ( [iId] => 2 [sName] => Jhon ) ) 然后通过这个数组,我创建POST请求,并返回X

我有一个这样的数组,我通过SQL查询得到它

Array
(
    [0] => Array
        (
            [iId] => 1
            [sName] => Tom
        )

    [1] => Array
        (
            [iId] => 2
            [sName] => Jhon
        )
)
然后通过这个数组,我创建POST请求,并返回XML对象

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [method] => userstats_xml
            [version] => 2.0
        )

    [userstats] => SimpleXMLElement Object
        (
            [type] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => 2
                                )

                            [test] => SimpleXMLElement Object
                                (
                                    [output] => 1280
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => 1
                                )

                            [test] => SimpleXMLElement Object
                                (
                                    [output] => 6112
                                )

                        )
                )
        )
)
现在我正试图得到如下所列的数组,因为我可以分别从第一个数组和xml对象输出数据,但我需要在一行中完成,例如

<a href="sId">sName - volume</a> 

您必须迭代XML响应并构建一个一维数组,其中iId作为键,XML的卷/输出作为值

$xmlOutput = 
Array
( 
  [1] => 6112
  [2] = > 1280
)
然后,可以使用上面的数组向从SQL获取的数组中添加新元素。请看下面的代码片段

 $sqlOutput =  
Array
    (
        [0] => Array
            (
                [iId] => 1
                [sName] => Tom
            )

        [1] => Array
            (
                [iId] => 2
                [sName] => Jhon
            )
    )
    foreach($sqlOutput as $entry)
    {
      $entry['volume'] = $xmlOutput[$entry['iId']];
    }

迭代数组并使用ID使用XPath查询XML以获得“输出”值

代码将获取数组中的每个ID,并从中构造一个XPath查询。查询将查找文档中属于测试元素的子元素的所有输出元素,该测试元素必须是SQL数组中具有ID属性的type元素的子元素。如果找到结果,输出元素的值将添加到SQL数组中当前迭代的位置


有关良好的XPath教程,请参阅。

到目前为止,您尝试了哪些内容?您必须手动解析XML,这是无法避免的。除了我们,你还需要为你编写解析器吗?我认为这很难理解。非常感谢,修改了你的答案,得到了我需要的东西!:)@Gordon
PHP注意:使用未定义的常量iId-假定为“iId”
?或者一般来说,代码是错误的。
 $sqlOutput =  
Array
    (
        [0] => Array
            (
                [iId] => 1
                [sName] => Tom
            )

        [1] => Array
            (
                [iId] => 2
                [sName] => Jhon
            )
    )
    foreach($sqlOutput as $entry)
    {
      $entry['volume'] = $xmlOutput[$entry['iId']];
    }
foreach ($peopleArray as $i => $person) {
    $xpathQuery = sprintf('//type[@id="%s"]/test/output', $person['iID']);
    $result = $xml->xpath($xpathQuery);
    if (isset($result[0])) {
        $peopleArray[$i]['volume'] = (string) $result[0];
    }
}