Php 防止SimpleXML合并重复的元素/标记?
我有一些来自avendor的带有重复元素的非标准XML SimpleXML和DOM都在合并我的XML标记。我怎样才能防止这种情况 有没有一种方法可以在不合并它们的情况下将它们放入一个数组 我的XML是这样的:Php 防止SimpleXML合并重复的元素/标记?,php,arrays,xml,dom,simplexml,Php,Arrays,Xml,Dom,Simplexml,我有一些来自avendor的带有重复元素的非标准XML SimpleXML和DOM都在合并我的XML标记。我怎样才能防止这种情况 有没有一种方法可以在不合并它们的情况下将它们放入一个数组 我的XML是这样的: 什么是1+1? 2. 4. 1. 0 0 > 1? 真的 假的 什么是2+1? 3. 4. 1. 0 我的PHP是这样的: 发生了什么事? 无论我如何尝试,SimpleXML都会将元素合并成一个数组(),如下所示: [questions] => Array ( [mult
什么是1+1?
2.
4.
1.
0
0 > 1?
真的
假的
什么是2+1?
3.
4.
1.
0
我的PHP是这样的:
发生了什么事?
无论我如何尝试,SimpleXML都会将
元素合并成一个数组()
,如下所示:
[questions] => Array
(
[multiplechoice] => Array
(
[0] => Array
(
[@attributes] => Array
(
[id] => 1}
)
[directions] => What is 1+1?
[answers] => Array
(
[answer] => Array
(
[0] => 2
[1] => 4
[2] => 1
[3] => 0
)
)
)
[1] => Array
(
[@attributes] => Array
(
[id] => 3
)
[directions] => What is 2+1?
[answers] => Array
(
[answer] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 0
)
)
)
)
[truefalse] => Array
(
[0] => Array
(
[@attributes] => Array
(
[id] => 2}
)
[directions] => 0 > 1?
[answers] => Array
(
[answer] => Array
(
[0] => True
[1] => False
)
)
)
)
)
我想要的是:
元素应分开,并按初始顺序排列:
[questions] => Array
(
[multiplechoice] => Array
(
[0] => Array
(
[@attributes] => Array
(
[id] => 1}
)
[directions] => What is 1+1?
[answers] => Array
(
[answer] => Array
(
[0] => 2
[1] => 4
[2] => 1
[3] => 0
)
)
)
)
[truefalse] => Array
(
[0] => Array
(
[@attributes] => Array
(
[id] => 2}
)
[directions] => 0 > 1?
[answers] => Array
(
[answer] => Array
(
[0] => True
[1] => False
)
)
)
)
[multiplechoice] => Array
(
[0] => Array
(
[@attributes] => Array
(
[id] => 3
)
[directions] => What is 2+1?
[answers] => Array
(
[answer] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 0
)
)
)
)
)
我不知道您如何使用DOM
,但它在这里保持了开箱即用的秩序:
$dom = new DOMDocument();
$dom->loadXML($xml);
$questions = $dom->getElementsByTagName('questions')->item(0);
if($questions instanceof DOMElement){
foreach($questions->childNodes as $child){
if($child instanceof DOMElement){
var_dump($child->tagName, simplexml_import_dom($child));
}
}
}
井然有序:
string(14) "multiplechoice"
class SimpleXMLElement#4 (3) {
public $@attributes =>
array(1) {
'id' =>
string(1) "1"
}
public $directions =>
string(12) "What is 1+1?"
public $answer =>
array(4) {
[0] =>
string(1) "2"
[1] =>
string(1) "4"
[2] =>
string(1) "1"
[3] =>
string(1) "0"
}
}
string(9) "truefalse"
class SimpleXMLElement#4 (3) {
public $@attributes =>
array(1) {
'id' =>
string(1) "2"
}
public $directions =>
string(6) "0 > 1?"
public $answer =>
array(2) {
[0] =>
string(4) "True"
[1] =>
string(5) "False"
}
}
string(14) "multiplechoice"
class SimpleXMLElement#4 (3) {
public $@attributes =>
array(1) {
'id' =>
string(1) "3"
}
public $directions =>
string(12) "What is 2+1?"
public $answer =>
array(4) {
[0] =>
string(1) "3"
[1] =>
string(1) "4"
[2] =>
string(1) "1"
[3] =>
string(1) "0"
}
正如包括我在内的其他人所概述的,不可能按照您在问题中要求的方式将树结构放入数组中 XML是一种树状结构,同一级别上可以存在多个同名元素。这是数组不可能实现的 因此,最好保持simplexmlement的原样。与处理阵列相比,它们在数据上的操作要舒服得多 然而,仍然可以用这些数据创建一个类似于您所要求的数组,但需要对其中的结构进行特殊处理。没有通用的方法来处理这个问题 例如:问题中的所有子元素位于其位置,然后键入其名称:
$result = $xml->xpath('//questions/*');
foreach ($result as &$rebuild) {
$rebuild = [$rebuild->getName() => $rebuild];
}
unset($rebuild);
$result = ['questions' => $result];
$array = json_decode(json_encode($result), true);
因此,生成的数组采用这种格式,我认为这种格式不太适合处理:
Array
(
[questions] => Array
(
[0] => Array
(
[multiplechoice] => Array
(
[@attributes] => Array
(
[id] => 1
)
[directions] => What is 1+1?
[answer] => Array
(
[0] => 2
[1] => 4
[2] => 1
[3] => 0
)
)
)
[1] => Array
(
[truefalse] => Array
(
[@attributes] => Array
(
[id] => 2
)
[directions] => 0 > 1?
[answer] => Array
(
[0] => True
[1] => False
)
)
)
[2] => Array
(
[multiplechoice] => Array
(
[@attributes] => Array
(
[id] => 3
)
[directions] => What is 2+1?
[answer] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 0
)
)
)
)
)
当然这是正常的,你不能有重复的密钥,所以它会将它们合并到其中。我的问题不是这样做是否正确,而是是否有可能解决这个问题。我一直在使用供应商提供的非标准XML…在数组/哈希映射中不能使用它。如果需要按顺序排列内容,请不要将其转换为那样的命名数组。如果您需要比简单XML提供更多的功能,请选择
DOMDocument
。请注意,此XML
不是非标准的,它非常有效,并且顺序在XML
中始终很重要。不要为此使用数组,直接使用XML对象。SimpleXMLElement或来自DOMDocument的元素。双方都保持秩序。@hakre就是这样,谢谢。@瑞肯给出了下面的完整答案。谢谢。谢谢,你已经完成了别人认为不可能的事情!
Array
(
[questions] => Array
(
[0] => Array
(
[multiplechoice] => Array
(
[@attributes] => Array
(
[id] => 1
)
[directions] => What is 1+1?
[answer] => Array
(
[0] => 2
[1] => 4
[2] => 1
[3] => 0
)
)
)
[1] => Array
(
[truefalse] => Array
(
[@attributes] => Array
(
[id] => 2
)
[directions] => 0 > 1?
[answer] => Array
(
[0] => True
[1] => False
)
)
)
[2] => Array
(
[multiplechoice] => Array
(
[@attributes] => Array
(
[id] => 3
)
[directions] => What is 2+1?
[answer] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 0
)
)
)
)
)