【发布时间】:2013-11-02 18:16:11
【问题描述】:
我有两个 xml 数组,我想将这些数组合并到第三个数组中……第一个 xml 结构是
$current = '<forms id="frm16648">
<group ref="" id="tarascioheader" mode="block">
<label>
<![CDATA[Group (tarascioheader)]]>
</label> structure u
<select ref="" id="petorresp">
<label>
<![CDATA[Select (petorresp)]]>
</label>
</select>
第二个数组是
$old = '<forms id="frm16648">
<group ref="" id="tarascioheader" mode="block">
<label>
<![CDATA[abc]]>
</label>
</group>
</forms>':
</group>
</forms>';
从这些 xmls 中,我想复制新数组中的所有匹配标签.... 我正在尝试通过递归函数来做到这一点......
function merge_xmls($current, $old)
{
$cxml = str_get_html($current);
$oxml = str_get_html($old);
do
{
$tt = $cxml->first_child();
if(!empty($tt) && !is_null($cxml->first_child()))
{
$x = $cxml->first_child();
$this->merge_xmls($x, $cxml, $oxml);
}
if(empty($tt))
{
$cid = $cxml->id;
$oid = $oxml -> find('#'.$cid);
if(!is_null($oid))
{
$cxml -> innerHTML = $oxml -> innerHTML;
}
}
$cxml = $cxml->next_sibling();
}
while(!empty($cxml) && !is_null($cxml));
}
【问题讨论】:
-
你的预期输出是什么?
-
PHP 中没有 XML 数组 之类的东西。你在说什么?也没有
$this- 请创建一个独立的工作示例,以尽可能少的数据和代码演示您的问题。 -
您是否尝试使用 SimpleXML?就像 hakre 所说,没有 XML 数组之类的东西,但您可以用一个数组构建一个数组。这就是你想要做的吗?
-
你的代码也很连贯。请删除那里的任何多余代码。给变量赋值一次就可以了,不需要重新调用同一个方法再赋值给另一个变量。还要给变量正确的名称。