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 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作