将 xml 转换为 php 数组空 xml 字段产生空数组而不是空字符串

Dzs*_*eti 6 php xml arrays json simplexml

我正在使用一段漂亮的短代码将我的 xml 字符串转换为 php 数组

$products = json_decode(json_encode(simplexml_load_string($products_xml)),TRUE);
Run Code Online (Sandbox Code Playgroud)

一切正常,除了当我得到一个空数组而不是字符串时 xml 字段为空时 - 当写入我的 SQL 数据库时,我看到字符串 'Array' 而不是空字段。

有没有一种很好的方法可以用 php 做到这一点?


与此同时,我设法用这段代码解决了这个问题:

$products = json_decode(str_replace('{}', '""', json_encode(simplexml_load_string($products_xml))),TRUE);
Run Code Online (Sandbox Code Playgroud)

Gfr*_*a54 0

如果 json_encoded xml 中的任何内容字段包含“{}”,则此代码将会中断。这发生在我身上,我正在研究解决方案来防止这种情况发生。

  • 解决办法在哪里? (4认同)