frankie
frankie

Reputation: 673

PHP - Why is this variable not working in this foreach loop?

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.

Upvotes: 0

Views: 225

Answers (1)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

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.

Upvotes: 2

Related Questions