duanji7182 2011-08-29 21:12
浏览 66
已采纳

如何在PHP中使用XML数据创建变量?

Been trying to figure this out for a short while now but having now luck, for example I have an external xml document like this:

<?xml version="1.0" ?>
<template>
    <name>My Template Name</name>
    <author>John Doe</author>
    <positions>
        <position>top-a</position>
        <position>top-b</position>
        <position>sidebar-a</position>
        <position>footer-a</position>
    </positions>
</template>

How can I process this document to create variables like this:

$top-a = top-a;
$top-b = top-b;
$sidebar-a = sidebar-a;
$footer-a = footer-a

If you can't make them into variables, how would you put them into an array?

Any help will be greatly appreciated.

  • 写回答

9条回答 默认 最新

  • dtypj3308 2011-08-29 21:23
    关注

    From the PHP web site at http://www.php.net/manual/en/function.xml-parse.php:

    Ashok dot 893 at gmail dot com 26-Apr-2010 05:52 This is very simple way to convert all applicable objects into associative array. This works with not only SimpleXML but any kind of object. The input can be either array or object. This function also takes an options parameter as array of indices to be excluded in the return array. And keep in mind, this returns only the array of non-static and accessible variables of the object since using the function get_object_vars().

    <?php
    function objectsIntoArray($arrObjData, $arrSkipIndices = array())
    {
        $arrData = array();
    
        // if input is object, convert into array
        if (is_object($arrObjData)) {
            $arrObjData = get_object_vars($arrObjData);
        }
    
        if (is_array($arrObjData)) {
            foreach ($arrObjData as $index => $value) {
                if (is_object($value) || is_array($value)) {
                    $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
                }
                if (in_array($index, $arrSkipIndices)) {
                    continue;
                }
                $arrData[$index] = $value;
            }
        }
        return $arrData;
    }
    ?>
    
    Usage:
    
    <?php
    $xmlUrl = "feed.xml"; // XML feed file/URL
    $xmlStr = file_get_contents($xmlUrl);
    $xmlObj = simplexml_load_string($xmlStr);
    $arrXml = objectsIntoArray($xmlObj);
    print_r($arrXml);
    ?>
    

    This will give the following result:

    Array
    (
        [name] => My Template Name
        [author] => John Doe
        [positions] => Array
            (
                [position] => Array
                    (
                        [0] => top-a
                        [1] => top-b
                        [2] => sidebar-a
                        [3] => footer-a
                    )
    
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改