PHP / SimpleXML – 为单个子项和多个子项生成不同的数组

前端之家收集整理的这篇文章主要介绍了PHP / SimpleXML – 为单个子项和多个子项生成不同的数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Simple XML来解析来自不同房地产经纪人的属性列表的XML提要. XML Feed的相关部分看起来像这样:
<branch name="Trustee Realtors">
    <properties>
        <property>
            <reference>1</reference>
            <price>275000</price>
            <bedrooms>3</bedrooms>
        </property>
        <property>
            <reference>2</reference>
            <price>350000</price>
            <bedrooms>4</bedrooms>
        </property>
        <property>
            <reference>3</reference>
            <price>128500</price>
            <bedrooms>4</bedrooms>
        </property>
    </properties>
</branch>
<branch name="Quick-E-Realty Inc">
    <properties>
        <property>
            <reference>4</reference>
            <price>180995</price>
            <bedrooms>3</bedrooms>
        </property>
    </properties>
</branch>

然后转换为这样的数组:

$xml = file_get_contents($filename);
$xml = simplexml_load_string($xml);
$xml_array = json_decode(json_encode((array) $xml),1);
$xml_array = array($xml->getName() => $xml_array);

我遇到的问题是,当创建数组时,单个列表的数据在数组中与多个列表的位置不同 – 我不确定如何解释这个,但如果我var_dump()它看起来像这样的多个项目的数组:

array(3) {
    [0]=>
    array(3) {
        ["reference"]=>
        string(4) "0001"
        ["price"]=>
        string(6) "275000"
        ["bedrooms"]=>
        int(3)
    }
    [1]=>
    array(3) {
        ["reference"]=>
        string(4) "0002"
        ["price"]=>
        string(6) "350000"
        ["bedrooms"]=>
        int(4)
    }
    [2]=>
    array(3) {
    ["reference"]=>
        string(4) "0003"
        ["price"]=>
        string(6) "128500"
        ["bedrooms"]=>
        int(2)
    }
}

如果我var_dump()单个列表的数组,它看起来像这样:

array(3) {
    ["reference"]=>
    string(4) "0004"
    ["price"]=>
    string(6) "180995"
    ["bedrooms"]=>
    int(3)
}

但我需要它看起来像这样:

array(1) {
    [0]=>
    array(3) {
        ["reference"]=>
        string(4) "0004"
        ["price"]=>
        string(6) "180995"
        ["bedrooms"]=>
        int(3)
    }
}

这些数组中的每一个都代表来自单个房地产经纪人的房产列表.我不确定这是否只是SimpleXML或json函数的工作方式,但我需要的是使用相同的格式(包含属性列表的数组是[0]键的值).

提前致谢!

SimpleXML像这样古怪.我最近使用它试图使配置文件“更容易”编写,并在过程中发现SimpleXML并不总是一致.在这种情况下,我认为您将受益于简单地检测< property>是一个集合中唯一的一个,如果是,那么将它自己包装在一个数组中,然后将它发送到你的循环中.

注意:[‘root’]是因为我需要包装’< root>< / root>‘ XML周围的元素,使我的测试工作.

//Rebuild the properties listings
$rebuild = array();
foreach($xml_array['root']['branch'] as $key => $branch) {
    $branchName = $branch['@attributes']['name'];
    //Check to see if 'properties' is only one,if it
    //is then wrap it in an array of its own.
    if(is_array($branch['properties']['property']) && !isset($branch['properties']['property'][0])) {
        //Only one propery found,wrap it in an array
        $rebuild[$branchName] = array($branch['properties']['property']);
    } else {
        //Multiple properties found
        $rebuild[$branchName] = $branch['properties']['property'];
    }
}

这需要重建您的属性.感觉有点hackish.但基本上你在这里检测到缺少多维数组:

if(is_array($branch['properties']['property']) && !isset($branch['properties']['property'][0]))

如果您没有找到多维数组,那么您明确地生成单个< property>中的一个.然后,为了测试所有内容都已正确重建,您可以使用以下代码

//Now do your operation...whatever it is.
foreach($rebuild as $branch => $properties) {
    print("Listings for $branch:\n");
    foreach($properties as $property) {
        print("Reference of " . $property['reference'] . " sells at $" . $property['price'] . " for " . $property['bedrooms'] . " bedrooms.\n");
    }
    print("\n");
}

这会产生以下输出

Listings for Trustee Realtors:
Reference of 1 sells at $275000 for 3 bedrooms.
Reference of 2 sells at $350000 for 4 bedrooms.
Reference of 3 sells at $128500 for 4 bedrooms.

Listings for Quick-E-Realty Inc:
Reference of 4 sells at $180995 for 3 bedrooms.

并且重建的转储将产生:

Array
(
    [Trustee Realtors] => Array
        (
            [0] => Array
                (
                    [reference] => 1
                    [price] => 275000
                    [bedrooms] => 3
                )

            [1] => Array
                (
                    [reference] => 2
                    [price] => 350000
                    [bedrooms] => 4
                )

            [2] => Array
                (
                    [reference] => 3
                    [price] => 128500
                    [bedrooms] => 4
                )

        )

    [Quick-E-Realty Inc] => Array
        (
            [0] => Array
                (
                    [reference] => 4
                    [price] => 180995
                    [bedrooms] => 3
                )

        )

)

我希望这可以帮助您更接近问题的解决方案.

原文链接:https://www.f2er.com/php/137872.html

猜你在找的PHP相关文章