I've got following XML:
我有下面的XML:
<account>
<id>123</id>
<email></email>
<status>ACTIVE</status>
</account>
I want to have it as an array variable. Therefore I read it with $xml = simplexml_load_file()
. The simplest way to convert simpleXMLElement to an associative array I know is to grind it with: json_decode(json_encode((array) $xml),1);
我想把它作为数组变量。因此,我使用$xml = simplexml_load_file()读取它。将simpleXMLElement转换为关联数组的最简单方法是:json_decode(json_encode(数组)$xml),1;
The problem is that I don't want to get the email
key as an empty array, but rather as a NULL
value. As SimpleXMLElement, it looks like:
问题是,我不希望将电子邮件键作为一个空数组,而是作为一个空值。SimpleXMLElement如下所示:
public 'email' =>
object(SimpleXMLElement)[205]
whereas in array it looks like:
而在数组中则是:
'email' =>
array (size=0)
empty
I'd like to get:
我想买:
'email' => NULL
The only way to achieve this I thought of is iterate through all elements and replace empty array with null value. The problem is that my XML is way bigger (above is just to explain the problem) and I'd have to iterate a lot of XML elements (and this would be manual work - I'm looking for something automatic). Maybe I'm missing some options in one of the functions... or maybe there's another trick to do this?
实现这一点的唯一方法是遍历所有元素,并用空值替换空数组。问题是,我的XML要大得多(上面只是为了解释问题),我必须迭代许多XML元素(这将是手工操作——我正在寻找一些自动的东西)。也许我在其中一个函数中漏掉了一些选项……或者可能还有别的方法?
2 个解决方案
#1
5
I cannot add a comment, but I think this will work for you, it should be faster then a regex or a loop:
我无法添加评论,但我认为这对您来说是可行的,它应该比regex或循环更快:
//after you json_encode, before you decode
$str = str_replace(':[]',':null',json_encode($array));
An empty array in JSON is represented by "[]
". Sometimes the arrays are parsed as objects, in that case (or as a fallback) you can replace ":{}
" too.
JSON中的空数组用“[]”表示。有时数组被解析为对象,在这种情况下(或者作为回退),您也可以替换“:{}”。
#2
1
Check performance srt_replace vs recursive loop
检查性能srt_replace vs递归循环
- Iterations: 100000
- 迭代:100000
- XML lenght: 4114 bytes
- XML长度:4114字节
- Init script time: ~1.2264486691986E-6 seconds
- 初始化脚本时间:~1.2264486691986E-6秒
- Json encode/decode time: ~9.8956169957496E-5 seconds
- Json编码/解码时间:~9.8956169957496E-5秒
- str_replace average time: 0.00010692856433176 seconds
- 更换平均时间:0.00010692856433176秒
- recursive loop average time: 0.00011844366600813 seconds
- 递归循环平均时间:0.00011844366600813秒
The str_replace quickly on ~0.00001 seconds. The difference will be noticeable in many calls
在~0.00001秒内快速替换str_replace。这种差别在很多电话中都很明显。
#1
5
I cannot add a comment, but I think this will work for you, it should be faster then a regex or a loop:
我无法添加评论,但我认为这对您来说是可行的,它应该比regex或循环更快:
//after you json_encode, before you decode
$str = str_replace(':[]',':null',json_encode($array));
An empty array in JSON is represented by "[]
". Sometimes the arrays are parsed as objects, in that case (or as a fallback) you can replace ":{}
" too.
JSON中的空数组用“[]”表示。有时数组被解析为对象,在这种情况下(或者作为回退),您也可以替换“:{}”。
#2
1
Check performance srt_replace vs recursive loop
检查性能srt_replace vs递归循环
- Iterations: 100000
- 迭代:100000
- XML lenght: 4114 bytes
- XML长度:4114字节
- Init script time: ~1.2264486691986E-6 seconds
- 初始化脚本时间:~1.2264486691986E-6秒
- Json encode/decode time: ~9.8956169957496E-5 seconds
- Json编码/解码时间:~9.8956169957496E-5秒
- str_replace average time: 0.00010692856433176 seconds
- 更换平均时间:0.00010692856433176秒
- recursive loop average time: 0.00011844366600813 seconds
- 递归循环平均时间:0.00011844366600813秒
The str_replace quickly on ~0.00001 seconds. The difference will be noticeable in many calls
在~0.00001秒内快速替换str_replace。这种差别在很多电话中都很明显。