【问题标题】:PHP (XML TO ARRAY) Getting the attribute of the xml that is arrayPHP(XML TO ARRAY)获取数组的xml属性
【发布时间】:2014-04-09 17:58:17
【问题描述】:

您好,我正在 spilgames xml 中获取游戏。我设法将其转换为数组,但又出现了 1 个问题,问题是我要获取的字符串位于数组内部或 1 个节点 xml 的属性中。我无法获取该属性。

我要获取属性。

spilgames 网站:here

到目前为止我的代码:

<?php

    $xml = simplexml_load_string(file_get_contents('http://publishers.spilgames.com/rss-3?limit=100&format=xml&category=Action')); /* Get the xml */
    $json = json_encode($xml);
    $games = json_decode($json); /* Make it an array */
    $game = $games->entries->entry;
    foreach($game as $entry) { /* Each game information */
        echo $entry->title;
        echo $entry->id;
        echo $entry->description;
        echo $entry->category;
        echo $entry->subcategory;
        echo $entry->technology;
        echo $entry->player->url;;
        echo $entry->thumbnails->small->url; /* Problems starts here */
   /* Because the thumbnails has 3 child but the info is inside of each child */
    }

?>

我在 spilgames 中得到的是 xml 而不是 json,因为我不知道 json 是如何工作的。

【问题讨论】:

    标签: php arrays xml json


    【解决方案1】:

    如果我是你,我会学习 JSON 的工作原理,因为它会让你的生活更轻松:

    <?php
    $json = file_get_contents('http://publishers.spilgames.com/rss-3?limit=100&format=json&category=Action');
    $data = json_decode($json);
    
    foreach ($data->entries as $entry) {
      echo $entry->title . "\n";
      echo $entry->id . "\n";
      echo $entry->description . "\n";
      echo $entry->category . "\n";
      echo $entry->subcategory . "\n";
      echo $entry->technology . "\n";
    
      echo $entry->thumbnails->small . "\n";
      echo $entry->thumbnails->medium . "\n";
      echo $entry->thumbnails->large . "\n";
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2013-02-22
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多