doqrjrc95405 2018-07-19 04:43
浏览 45
已采纳

将多维SimpleXML转换为关联数组

I have a nested multidimensional XML string that I get into SimpleXML. I want to convert it into an associative array. The examples listed on php.net do not work correctly or they do only for flat xmls.

  • 写回答

1条回答 默认 最新

  • dourong8495 2018-07-19 04:43
    关注

    This works better than the example on SimpleXML manual page, but in its current form it discards the attributes.

    function xml2array($xmlObject, $out = array())
    {
        foreach ($xmlObject as $node) {
            if ($node->count() > 0) {
                $out[$node->getName()][] = xml2array($node);
            } else {
                $out[$node->getName()] = (string)$node;
            }
        }
        return $out;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部