dopr25398 2011-05-12 22:58
浏览 58
已采纳

PHP - 数组项的Echo键值

I need to echo the key value of an array.

This is how im outputting my array:

foreach($xml->channel->item as $item) {
  $item->title
}

I've tried adding:

foreach($xml->channel->item as $key => $item)

But the value I echo just comes out as: item

Any ideas?

Var Dump of item results:

var_dump($xml->channel->item);

    object(SimpleXMLElement)#4 (5) { 
["title"]=>  string(33) "BA and union agree to end dispute" 
["description"]=>  string(118) "British Airways and the Unite union reach an agreement which could end the long-running dispute between the two sides." 
["link"]=>  string(61) "http://www.bbc.co.uk/go/rss/int/news/-/news/business-13373638" 
["guid"]=>  string(43) "http://www.bbc.co.uk/news/business-13373638" 
["pubDate"]=>  string(29) "Thu, 12 May 2011 12:33:38 GMT" 
} 
  • 写回答

4条回答 默认 最新

  • douchao1864 2011-05-12 23:06
    关注

    Try doing:

    $data = $xml->channel->item;
    
    if (is_object($data) === true)
    {
        $data = get_object_vars($data);
    }
    
    echo '<pre>';
    print_r(array_keys($data));
    echo '</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数