записи цикла simpleXML - PullRequest
       3

записи цикла simpleXML

0 голосов
/ 19 декабря 2011

у меня есть xml ниже

    <entry>
        <id></id>
        <title></title>
        <dc:identifier></dc:identifier>
        <dc:identifier></dc:identifier>
        <dc:relation></dc:relation>
        <dc:publisher></dc:publisher>
        <dc:language xsi:type="dcterms:ISO639-2"></dc:language>
        <dcterms:issued></dcterms:issued>
        <updated></updated>
        <rights></rights>
        <author>
            <name></name>
            <uri></uri>
        </author>
        <link rel="alternate" href="" type="text/html"/>
        <link rel="http://opds-spec.org/image" href="" type="image/jpeg"/>
        <link rel="http://opds-spec.org/image/thumbnail" href="" type="image/jpeg"/>
        <link rel="http://opds-spec.org/acquisition/buy" href="" type="application/epub+zip">
            <opds:price currencycode="EUR"></opds:price>
        </link>
        <summary type="text"></summary>
</entry>
<entry>
        <id></id>
        <title></title>
        <dc:identifier></dc:identifier>
        <dc:identifier></dc:identifier>
        <dc:relation></dc:relation>
        <dc:publisher></dc:publisher>
        <dc:language xsi:type="dcterms:ISO639-2"></dc:language>
        <dcterms:issued></dcterms:issued>
        <updated></updated>
        <rights></rights>
        <author>
            <name></name>
            <uri></uri>
        </author>
        <link rel="alternate" href="" type="text/html"/>
        <link rel="http://opds-spec.org/image" href="" type="image/jpeg"/>
        <link rel="http://opds-spec.org/image/thumbnail" href="" type="image/jpeg"/>
        <link rel="http://opds-spec.org/acquisition/buy" href="" type="application/epub+zip">
            <opds:price currencycode="EUR"></opds:price>
        </link>
        <summary type="text"></summary>
</entry>
......

Я могу хорошо разобрать при разборе всех элементов (simpleXML)

Теперь я хочу получить один элемент за раз. Так что я передаю переменную в мой файл, как

file.php?start=1
file.php?start=2
etc

и я попытался получить доступ к текущему элементу

$xml->entry[$start] returns NULL

OR

 $xml->entry->{$start} returns NULL

но оба возвращают NULL

Я пытался получить к нему доступ напрямую

$xml->entry[1]
$xml->entry->{1}

и работает нормально

Я что-то упустил?

Ответы [ 2 ]

1 голос
/ 19 декабря 2011
<?php
$xml = simplexml_load_file('simplexml.php');

$id = '1';
print_r($xml->entry[$id]);
?>

Результат:

NULL

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

$id = '1';
$id = (int)$id;

print_r($xml->entry[$id]);

?>

Результат:

SimpleXMLElement Object ( [entry] => SimpleXMLElement Object ( [id] => 2 [title] => Другое название ) )

1 голос
/ 19 декабря 2011

$ _ GET ['start'], возможно, неверный тип переменной, попробуйте приведение типа к целому.

...