I have a simple problem getting data from an eBay API string. I want to format numbers to 2 digits 8.0 > 8.00
This works fine
echo $price; // output: 8.0
But…
echo number_format($price, 2); // output: (nothing)
A var_dump tells me why…
var_dump($price);
// output: object(SimpleXMLElement)#19 (2) { ["@attributes"]=> array(1) { ["currencyId"]=> string(3) "USD" } [0]=> string(3) "8.0" }
How do I get the 8.0 into a 8.00 (I know I can use REGEX but it feels like not the proper way)
And while we are here, how I can get the ‘USD’ ?
PS: the API call used is findCompletedItems – and strangely to me, the XML response has no visible USD at all.
2
Answers
You’re not passing in a string, you’re passing in an object of class
SimpleXMLElement
. The easiest you can do is cast it to a string before passing it to number_format using(string)$price
The var_dump gives you an object of type SimpleXMLElement which has a __toString method which returns the text content that is directly in the element so
echo $price;
will result in8.0
The
USD
is part of the attributes which returns an object of type SimpleXMLElement.You can get the price and the currency casting it to a
(string)