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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?