1

我一直在尝试创建一个foreach循环,该循环ListingId将从 XML 文件中获取所有内容,但由于某种原因,我只能获取一个“Id”

XML结构:

<List>
    <Listing>
        <ListingId></ListingId>
</Listing>
    <Listing>
        <ListingId></ListingId>
</Listing>
    <Listing>
        <ListingId></ListingId>
</Listing>
</List>

PHP:

$xml = simplexml_load_file("domain.co.nz/xml.xml");

        foreach($xml->List->Listing->ListingId as $value)
        {

            $xmlArray[] = $value;
        }
4

1 回答 1

1
<?php
$xml = '<List>
    <Listing>
        <ListingId>12</ListingId>
</Listing>
    <Listing>
        <ListingId>13</ListingId>
</Listing>
    <Listing>
        <ListingId>14</ListingId>
</Listing>
</List>';


$xml = simplexml_load_string($xml);
$arr = array();

foreach($xml->Listing as $value)
{
    $arr[] = intval($value->ListingId);
}

print_r($arr); //Array ( [0] => 12 [1] => 13 [2] => 14 )
于 2012-11-14T02:25:04.603 回答