dourao1877 2012-11-05 20:19
浏览 76
已采纳

对于每个密钥不是一个数字

So I'm trying to iterate through an XML feed and paginate it but I've run into a problem. When I try to get the index (key) of the current array it outputs a string "campDetails" on every iteration instead of an incrementing integer like 0,1,2,3

Here is a sample of the XML format

<campaigns>
<campDetails>
<campaign_id>2001</campaign_id>
<campaign_name>Video Chat Software</campaign_name>
<url>http://www.fakeurl.com</url>
</campDetails>
<?php
 $call_url = "https://www.fakeurl.com";
  if($xml = simplexml_load_file($call_url, "SimpleXMLElement", LIBXML_NOCDATA)):

           foreach($xml as $i => $offers):
                $offer_link     = $offers->url;
                $offer_raw_name = $offers->campaign_name;
              echo $i . " " . $offer_link; ?> </br> <?php echo $offer_raw_name;
           endforeach;
  endif;
  ?>

Output to be expected:

0 http://www.fakeurl.com
Video Chat Software

Actual output:

campDetails http://www.fakeurl.com
Video Chat Software

EDIT: Thank you for your answers everyone. It seems I was given incorrect information from another question on here. I was told that $i would keep a numerical index for the current iteration.

print_r($xml); (obviously more results but this is the first)

SimpleXMLElement Object ( [campDetails] => Array ( [0] => SimpleXMLElement Object ( [campaign_id] => 2001 [campaign_name] => Video Chat Software [url] => http://www.fakeurl.com/ )
  • 写回答

5条回答 默认 最新

  • doulan1866 2012-12-09 00:16
    关注

    Instead of using the current foreach and associative array, I pushed all values to an indexed array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题