简体   繁体   English

SimpleXML 到数组

[英]SimpleXML to Array

I load some XML which produces a SimpleXML Object like so (only displaying one Object)我加载了一些像这样产生 SimpleXML 对象的 XML(只显示一个对象)

SimpleXMLElement Object
(
    [Jobs] => SimpleXMLElement Object
    (
        [Job] => Array
            (
                [0] => SimpleXMLElement Object
                (
                    [ID] => J000001
                    [Name] => Internal Time
                    [Description] => Use this job to record your internal and non-billable time for activities such as annual leave, sick leave, professional development, staff meetings etc
                    [Client] => SimpleXMLElement Object
                    (
                        [ID] => 8430219
                        [Name] => Fake Client
                    )

                    [ClientOrderNumber] => SimpleXMLElement Object
                    (
                    )

                    [State] => Planned
                    [StartDate] => 2016-03-21T00:00:00
                    [DueDate] => 2017-03-21T00:00:00
                    [InternalID] => 11442733
                    [Assigned] => SimpleXMLElement Object
                    (
                        [Staff] => SimpleXMLElement Object
                            (
                                [ID] => 344460
                                [Name] => Som Name
                            )
                    )
                )
            )
    )
)

What I would like to do is create an array of the client Name and client ID.我想做的是创建一个包含客户端名称和客户端 ID 的数组。 So I am aiming for something like so所以我的目标是这样的

[data] => array (
    8430219 => Fake Client,
    8430343 => Another Client,
    etc
)

At the moment, I can get the name in place, but struggling with the id because it says it is an illegal offset type.目前,我可以得到名称,但与 id 斗争,因为它说它是非法偏移类型。 This is what I have这就是我所拥有的

foreach($oXML->Jobs as $oEntry) {
    foreach ($oEntry->Job as $data) {
        $jobsArray = array(
            $data->Client->ID => $data->Client->Name
        );
    }
}

How can I create the array based on the SimpleXML Object I have?如何根据我拥有的 SimpleXML 对象创建数组?

Thanks谢谢

First of all, to obtain your desired array you can not use this syntax:首先,要获得所需的数组,您不能使用以下语法:

$jobsArray = array( $key => $val );

You have to use something like this:你必须使用这样的东西:

$jobsArray[$key] = $val;

Otherwise, at each repeating loop, your syntax will override precedent array values.否则,在每个重复循环中,您的语法将覆盖先前的数组值。

Then, I suggest you to use XPath to simplify entire process.那么,我建议你使用 XPath 来简化整个过程。 With XPath queries, you can retrieve a set of nodes with only one search.使用 XPath 查询,您只需一次搜索即可检索一组节点。

Assuming you have this XML:假设你有这个 XML:

<?xml version="1.0"?>
<Jobs>
    <Job>
        <Client>
            <ID>8430219</ID>
            <Name>Fake Client</Name>
        </Client>
        <Client>
            <ID>8430220</ID>
            <Name>Fake Client 2</Name>
        </Client>
    </Job>
    <Job>
        <Client>
            <ID>8430221</ID>
            <Name>Fake Client 3</Name>
        </Client>
    </Job>
</Jobs>

With this xpath query:使用此 xpath 查询:

$clients = $oXML->xpath( '/Jobs/Job/Client' );

you obtain in $clients all <Client> nodes.您在$clients获得所有<Client>节点。 Then you can create your array in this way:然后你可以用这种方式创建你的数组:

$result = array();
foreach( $clients as $client )
{
    $result[$client->ID->__toString()] = $client->Name->__toString();
}

This is $result after foreach loop:这是foreach循环后的$result

Array
(
    [8430219] => Fake Client
    [8430220] => Fake Client 2
    [8430221] => Fake Client 3
)

Note that we have to cast as string single nodes (that are SimpleXML objects): this is absolutely necessary creating keys, otherwise the array assignment fails.请注意,我们必须将单个节点(即 SimpleXML 对象)转换为字符串:这是创建键绝对必要的,否则数组分配将失败。 You can obtain same result using (string) $client->ID instead of $client->ID->__toString()您可以使用(string) $client->ID而不是$client->ID->__toString()获得相同的结果


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM