dsceme82487 2013-03-15 22:59
浏览 21
已采纳

PHP - 为什么这个变量在这个foreach循环中不起作用?

The $value prints correctly. The number(s) are correct for $value so I think that part is eliminated.

If I manually enter the actual numbers in ($value)->price like (10079)->price, the function works fine and the last line print_r ($price) prints the number it supposed to.

For some reason $value is not working in the context of $xml_price = $fetch_app->products($value)->price; as the function returns nil for $price

foreach ($_SESSION['queueList'] as $value){
            //this prints the correct item(s) in 'queueList'
            print_r ($value);
            //this gets the node with the price info
            $xml_price = $fetch_app->products($value)->price;
            //this converts the simpleXML node to a string
            $price = ((string) $xml_price);
            //session var accumulates the item prices in cart
            $_SESSION['totalPrice'] += $price;
            print_r ($price);

        }

So why is the $value variable not working, but an actual number does, even though I have printed the $value and it shows the correct number? The number is a float by the way, not sure if that matters.

  • 写回答

1条回答 默认 最新

  • dongseshu0698 2013-03-15 23:12
    关注

    Judging by the additional information from the comments, the following should work:

    $xml_price = $fetch_app->products((int)$value)->price;
    

    It looks like this fetchapp API is strongly typed, which is untypical for PHP but still technically possible to a certain extent. At least it treats string parameters different from integer parameters.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么