foreach声明错误

时间:2011-07-08 18:26:39

标签: php foreach simplexml

有人能告诉我这段代码有什么问题吗?

<?php
$feedID = '28241415';
$oddsArray = array();
$source = file_get_contents("https://www.bwin.com/partner/xml/query.aspx?source=events&lid=1&xpath=/ROOT/EVENTS/E[@LID=46%20and%20@RID=14%20and%20@SID=4]");
$xml = simplexml_load_string($source);
$game = $xml->xpath("//G");

foreach ($game as $event)
{
    if ($event['DBID'] == $feedID)
        {
            foreach ($event->children() as $odds) 
                {
                    array_push($oddsArray, array('oddsID' => $odds['DBID'], 'odds' => $odds['O']));
                }
        }
}
foreach ($array as $oddsArray)
{
echo $array['odds'];
echo $array['oddsID'];
}
?>

我收到的错误是:

  

警告:/home/pokerint/public_html/test.php中为foreach()提供的参数无效

2 个答案:

答案 0 :(得分:6)

我在这里猜测问题不是很清楚,但我认为你需要在最后一个foreach中颠倒变量的顺序

foreach ($array as $oddsArray)
{
echo $array['odds'];
echo $array['oddsID'];
}

foreach ($oddsArray as $array)
{
echo $array['odds'];
echo $array['oddsID'];
}

答案 1 :(得分:0)

foreach ($array as $oddsArray)

我认为你倒退了......

为避免混淆,我总是使用容易记住的东西......

foreach ($oddsArray as $key => $value) {

OR

foreach ($oddsArray as $value) {
甚至......

然后你永远不会混淆(希望永远不会:)