如何将xml对象推送到数组?

时间:2016-07-19 10:57:55

标签: php xml

我想将每个coa推送到array.here是xml文件代码。

<?xml version="1.0" encoding="utf-8"?>
<companies>
    <company>
        <name>Best Western</name>
        <groups>
            <group>
                <coa>410101 · Net Room Revenue Taxable</coa>
                <coa>415101 · GTD No Show</coa>
                <coa>Total 400000 · ROOM REVENUE</coa>
                <coa>Total I · ROOM REVENUE</coa>
                <coa>II · OTHER OPERATING REVENUE</coa>
                <coa>425120 · Meeting Room</coa>
                <coa>480380 · Interest Income</coa>
                <coa>480383 · Guest Laundry</coa>
                <coa>480385 · Vending</coa>
                <coa>480389 · Miscellaneous</coa>
                <coa>482000 · Sales Tax Discounts</coa>
                <coa>Total 480000 · OTHER INCOME</coa>
                <coa>Total II · OTHER OPERATING REVENUE</coa>
                <coa>Total Income</coa>
            </group>
            <group>
                <coa>10 · ROOMS DEPARTMENT</coa>
                <coa>Total Income</coa>
            </group>
        </groups>
    </company>
</companies>

现在我尝试了这段代码

$xml = simplexml_load_file($url);
foreach ($xml as $company) {
$groups = $company->groups;
    $coa_list = $groups->group;
    $coa_array = array();
    foreach ($coa_list as $coal) {
        $coa_array = array();
        $coa_k = $coal->coa;
        foreach ($coa_k as $key => $value) {
            $val = $value;
            array_push($coa_array, $val);
        }
    }
}

实际上它应该可以工作,但我不知道为什么但它不起作用。

注意:我正在阅读xml文件。

当我在阵列推送之前打印$ val时,它会打印但不能将其推入阵列。

那么你可以告诉我我的代码有什么问题

由于

4 个答案:

答案 0 :(得分:1)

酷技巧:json_encode可以编码SimpleXml对象,json_decode可以用来将它们转换为数组:

$json = json_encode(simplexml_load_string($url));
$array = json_decode($json,true);
$coaGroups = array_column($array['company']['groups']['group'],'coa');

Live demo

答案 1 :(得分:0)

问题在于数组声明。因为每次它都会创建相同的数组,因为它的内部for循环,使数组声明动态

答案 2 :(得分:0)

您需要在代码之外移动数组声明:

文件 test.xml

<?xml version="1.0" encoding="utf-8"?>
<companies>
    <company>
        <name>Best Western</name>
        <groups>
            <group>
                <coa>410101 · Net Room Revenue Taxable</coa>
                <coa>415101 · GTD No Show</coa>
                <coa>Total 400000 · ROOM REVENUE</coa>
                <coa>Total I · ROOM REVENUE</coa>
                <coa>II · OTHER OPERATING REVENUE</coa>
                <coa>425120 · Meeting Room</coa>
                <coa>480380 · Interest Income</coa>
                <coa>480383 · Guest Laundry</coa>
                <coa>480385 · Vending</coa>
                <coa>480389 · Miscellaneous</coa>
                <coa>482000 · Sales Tax Discounts</coa>
                <coa>Total 480000 · OTHER INCOME</coa>
                <coa>Total II · OTHER OPERATING REVENUE</coa>
                <coa>Total Income</coa>
            </group>
            <group>
                <coa>10 · ROOMS DEPARTMENT</coa>
                <coa>Total Income</coa>
            </group>
        </groups>
    </company>
</companies>

php代码

<?php
$xml  = simplexml_load_file("test.xml");

$coa_array = array(); 
foreach ($xml as $company) { 
    $groups = $company->groups;
    $coa_list = $groups->group;    
    foreach ($coa_list as $coal) {
        $coa_k = $coal->coa;
        foreach ($coa_k as $key => $value) {
            $val = $value;
            array_push($coa_array, $val);
        }
    }
}

print_r($coa_array);

打印:

Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => 410101 · Net Room Revenue Taxable
        )

    [1] => SimpleXMLElement Object
        (
            [0] => 415101 · GTD No Show
        )

    [2] => SimpleXMLElement Object
        (
            [0] => Total 400000 · ROOM REVENUE
        )

    [3] => SimpleXMLElement Object
        (
            [0] => Total I · ROOM REVENUE
        )

    [4] => SimpleXMLElement Object
        (
            [0] => II · OTHER OPERATING REVENUE
        )

    [5] => SimpleXMLElement Object
        (
            [0] => 425120 · Meeting Room
        )

    [6] => SimpleXMLElement Object
        (
            [0] => 480380 · Interest Income
        )

    [7] => SimpleXMLElement Object
        (
            [0] => 480383 · Guest Laundry
        )

    [8] => SimpleXMLElement Object
        (
            [0] => 480385 · Vending
        )

    [9] => SimpleXMLElement Object
        (
            [0] => 480389 · Miscellaneous
        )

    [10] => SimpleXMLElement Object
        (
            [0] => 482000 · Sales Tax Discounts
        )

    [11] => SimpleXMLElement Object
        (
            [0] => Total 480000 · OTHER INCOME
        )

    [12] => SimpleXMLElement Object
        (
            [0] => Total II · OTHER OPERATING REVENUE
        )

    [13] => SimpleXMLElement Object
        (
            [0] => Total Income
        )

    [14] => SimpleXMLElement Object
        (
            [0] => 10 · ROOMS DEPARTMENT
        )

    [15] => SimpleXMLElement Object
        (
            [0] => Total Income
        )

)

如果您希望每个组在数组中创建一个新条目,您可以按如下方式修改代码:

<?php
$xml  = simplexml_load_file("test.xml");

$group_array = array(); 
foreach ($xml as $company) { 
    $groups = $company->groups;
    $coa_list = $groups->group;    
    foreach ($coa_list as $coal) {
        $coa_k = $coal->coa;
        $coa_array = [];
        foreach ($coa_k as $key => $value) {
            $val = $value;
            array_push($coa_array, $val);
        }
        array_push($group_array, $coa_array);
    }
}

print_r($group_array);

打印:

Array
(
    [0] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => 410101 · Net Room Revenue Taxable
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => 415101 · GTD No Show
                )

            [2] => SimpleXMLElement Object
                (
                    [0] => Total 400000 · ROOM REVENUE
                )

            [3] => SimpleXMLElement Object
                (
                    [0] => Total I · ROOM REVENUE
                )

            [4] => SimpleXMLElement Object
                (
                    [0] => II · OTHER OPERATING REVENUE
                )

            [5] => SimpleXMLElement Object
                (
                    [0] => 425120 · Meeting Room
                )

            [6] => SimpleXMLElement Object
                (
                    [0] => 480380 · Interest Income
                )

            [7] => SimpleXMLElement Object
                (
                    [0] => 480383 · Guest Laundry
                )

            [8] => SimpleXMLElement Object
                (
                    [0] => 480385 · Vending
                )

            [9] => SimpleXMLElement Object
                (
                    [0] => 480389 · Miscellaneous
                )

            [10] => SimpleXMLElement Object
                (
                    [0] => 482000 · Sales Tax Discounts
                )

            [11] => SimpleXMLElement Object
                (
                    [0] => Total 480000 · OTHER INCOME
                )

            [12] => SimpleXMLElement Object
                (
                    [0] => Total II · OTHER OPERATING REVENUE
                )

            [13] => SimpleXMLElement Object
                (
                    [0] => Total Income
                )

        )

    [1] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => 10 · ROOMS DEPARTMENT
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => Total Income
                )

        )

)

答案 3 :(得分:0)

文件example.xml

<?xml version="1.0" encoding="utf-8"?>
<companies>
    <company>
        <name>Best Western</name>
        <groups>
            <group>
                <coa>410101 · Net Room Revenue Taxable</coa>
                <coa>415101 · GTD No Show</coa>
                <coa>Total 400000 · ROOM REVENUE</coa>
                <coa>Total I · ROOM REVENUE</coa>
                <coa>II · OTHER OPERATING REVENUE</coa>
                <coa>425120 · Meeting Room</coa>
                <coa>480380 · Interest Income</coa>
                <coa>480383 · Guest Laundry</coa>
                <coa>480385 · Vending</coa>
                <coa>480389 · Miscellaneous</coa>
                <coa>482000 · Sales Tax Discounts</coa>
                <coa>Total 480000 · OTHER INCOME</coa>
                <coa>Total II · OTHER OPERATING REVENUE</coa>
                <coa>Total Income</coa>
            </group>
            <group>
                <coa>10 · ROOMS DEPARTMENT</coa>
                <coa>Total Income</coa>
            </group>
        </groups>
    </company>
</companies>

这是用于获取coa的php代码,您必须将simplexmlobject转换为数组。

<?php
    $xml = simplexml_load_file('example.xml');

    // echo "<pre>";
    // print_r($xml);
    // echo "</pre>";

    $coa_array = array(); 
    foreach ($xml as $company) { 
        $groups = $company->groups;
        $coa_list = $groups->group;    
        foreach ($coa_list as $coal) {
            $coa_k = $coal->coa;
            foreach ($coa_k as $key => $value) {
                $val = xml2array($value);
                // echo "<pre>";
                // print_r($val[0]);
                // echo "</pre>";
                // $val[]
                array_push($coa_array, $val[0]);
            }
        }
    }

    echo "<pre>";
    print_r($coa_array);
    echo "</pre>";

    function xml2array( $xmlObject, $out = array () ){
        foreach ( (array) $xmlObject as $index => $node )
            $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;

        return $out;
    }
?>

结果将是

Array
(
    [0] => 410101 · Net Room Revenue Taxable
    [1] => 415101 · GTD No Show
    [2] => Total 400000 · ROOM REVENUE
    [3] => Total I · ROOM REVENUE
    [4] => II · OTHER OPERATING REVENUE
    [5] => 425120 · Meeting Room
    [6] => 480380 · Interest Income
    [7] => 480383 · Guest Laundry
    [8] => 480385 · Vending
    [9] => 480389 · Miscellaneous
    [10] => 482000 · Sales Tax Discounts
    [11] => Total 480000 · OTHER INCOME
    [12] => Total II · OTHER OPERATING REVENUE
    [13] => Total Income
    [14] => 10 · ROOMS DEPARTMENT
    [15] => Total Income
)