PHP:从API的var_dump中打印一个值


PHP: Print one value from var_dump from API

我正在使用PHP进行API调用。PHP脚本使用SOAPJSON-RPC。它确实有效,但我很难从脚本中只打印一个值,而不是整个数据转储。从我读到的内容来看,var_dump返回变量、类型和值。我只想提取一个变量的值。

我的脚本的结尾是:

$jsonRpcRequest = array (
'method' => 'login',
'params' => array($merchantCode, $now, $hash),
'jsonrpc' => '2.0'
);
$ID = callRPC((Object)$jsonRpcRequest, $ApiUrl, true);
$productID = 4627344;
try {
$ProdID = $client->getProductById($ID, $productID);
} catch (SoapFault $e) {
echo "Product ID: " . $e->getMessage();
}
var_dump ($ProdID) ;

该脚本在我获取该产品id的所有产品数据时工作。但是,我只想打印价格的值,而不是整个转储。

脚本结果:

object(stdClass)#2 (24) {["ProductId"]=> int(4627344)["ProductEnabled"]=> bool(true) ["ProductType"]=> string(7) "REGULAR" ["ProductVersion"]=> string(0) "" ["Price"]=> float(1450) ["Currency"]=> string(3) "USD"

我试过使用网上的各种例子,但我似乎不能只说出价格。

所需结果:1450

就像对待任何普通对象一样,使用->箭头表示法访问属性:

$price = $ProdID->Price;
echo $price;