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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)