php - 从 PHP Web 服务输出中形成变量
问题描述
我正在尝试从从 Web 服务接收到的输出中形成变量。Web 服务在将车辆的 VIN 输入系统后提供有关车辆的详细信息。下面是输入 VIN 后接收到的输出的一部分。
object(stdClass)#2 (14) {
["responseStatus"]=>
object(stdClass)#3 (2) {
["responseCode"]=>
string(10) "Successful"
["description"]=>
string(10) "Successful"
}
["vinDescription"]=>
object(stdClass)#4 (11) {
["WorldManufacturerIdentifier"]=>
string(17) "Germany Audi Nsu "
}
["vin"]=>
string(17) "WAUUL78E38A092113"
["modelYear"]=>
int(2008)
["division"]=>
string(4) "Audi"
["modelName"]=>
string(2) "S4"
["styleName"]=>
string(13) "5dr Avant Wgn"
["bodyType"]=>
string(11) "Wagon 4 Dr."
["drivingWheels"]=>
string(3) "AWD"
["builddata"]=>
string(2) "no"
}
["style"]=>
array(2) {
[0]=>
object(stdClass)#26 (17) {
["division"]=>
object(stdClass)#27 (2) {
["_"]=>
string(4) "Audi"
["id"]=>
int(4)
}
["subdivision"]=>
object(stdClass)#28 (2) {
["_"]=>
string(4) "Audi"
["id"]=>
int(5020)
}
["model"]=>
object(stdClass)#29 (2) {
["_"]=>
string(2) "S4"
["id"]=>
int(17308)
}
["basePrice"]=>
object(stdClass)#30 (4) {
["unknown"]=>
bool(false)
["invoice"]=>
float(46137)
["msrp"]=>
float(49610)
["destination"]=>
float(775)
}
["bodyType"]=>
object(stdClass)#31 (3) {
["_"]=>
string(13) "Station Wagon"
["id"]=>
int(7)
["primary"]=>
bool(true)
}
["marketClass"]=>
object(stdClass)#32 (2) {
["_"]=>
string(11) "Small Wagon"
["id"]=>
int(53)
}
["acode"]=>
object(stdClass)#33 (1) {
["_"]=>
string(13) "USB80AUC085A0"
}
["id"]=>
int(292015)
["modelYear"]=>
int(2008)
["name"]=>
string(17) "5dr Avant Wgn Man"
["nameWoTrim"]=>
string(17) "5dr Avant Wgn Man"
["mfrModelCode"]=>
string(6) "8ED549"
["fleetOnly"]=>
bool(false)
["modelFleet"]=>
bool(false)
["passDoors"]=>
int(4)
["altBodyType"]=>
string(13) "Station Wagon"
["drivetrain"]=>
string(15) "All Wheel Drive"
}
到目前为止,我已经成功地创建和调用存储为对象的变量。
这是我到目前为止所做的。
根据输出创建变量:
$manuf = $result->vinDescription->WorldManufacturerIdentifier;
$vin = $result->vinDescription->vin;
$year = $result->vinDescription->modelYear;
$make = $result->vinDescription->division;
$model = $result->vinDescription->modelName;
$style = $result->vinDescription->styleName;
$body= $result->vinDescription->bodyType;
打印变量以描述车辆:
echo "Manufacturer: ".$manuf;
echo "<br>";
echo "Year: ".$year;
echo "<br>";
echo "Make: ".$make;
echo "<br>";
echo "Model: ".$model;
echo "<br>";
echo "Trim: ".$style;
echo "<br>";
echo "Body style: ".$body;
输出:
Manufacturer: Germany Audi Nsu
Year: 2008
Make: Audi
Model: S4
Trim: 5dr Avant Wgn
Body style: Wagon 4 Dr.
Drive type: Small Wagon
我无法创建和调用某些似乎嵌套在数组中的变量。我有兴趣创建的两个变量是门数和传动系统。它们位于名为passDoors
and的原始输出的底部drivetrain
。
我试图创建这样的变量:$drivetype = $result['style']['drivetrain'];
然而,我没有成功。关于如何从原始输出中创建passDoors
和调用的任何想法?drivetrain
任何反馈表示赞赏。
遇到另一个障碍。似乎我越深入数据,调用数据来创建新变量就越复杂。这是我正在努力的部分:
["technicalSpecification"]=>
array(97) {
[0]=>
object(stdClass)#640 (2) {
["titleId"]=>
int(1)
["value"]=>
array(2) {
[0]=>
object(stdClass)#641 (3) {
["styleId"]=>
array(2) {
[0]=>
int(292015)
[1]=>
int(292016)
}
["value"]=>
string(7) "Audi S4"
["condition"]=>
string(3) "-PT"
}
[1]=>
object(stdClass)#642 (3) {
["styleId"]=>
array(2) {
[0]=>
int(292015)
[1]=>
int(292016)
}
["value"]=>
string(7) "Audi S4"
["condition"]=>
string(0) ""
}
}
}
[1]=>
object(stdClass)#643 (2) {
["titleId"]=>
int(2)
["value"]=>
object(stdClass)#644 (3) {
["styleId"]=>
array(2) {
[0]=>
int(292015)
[1]=>
int(292016)
}
["value"]=>
string(12) "5 Door Wagon"
["condition"]=>
string(0) ""
}
}
[2]=>
object(stdClass)#645 (2) {
["titleId"]=>
int(6)
["value"]=>
object(stdClass)#646 (3) {
["styleId"]=>
array(2) {
[0]=>
int(292015)
[1]=>
int(292016)
}
["value"]=>
string(15) "All-Wheel Drive"
["condition"]=>
string(0) ""
}
}
我正在尝试提取“Audi S4”、“5 Door Wagon”和“All-Wheel Drive”
非常感谢您的任何意见。
解决方案
Whilestyle
是一个数组,您应该将其作为数组处理。似乎可以传递多种样式。所以不是一个传动系统,而是多个传动系统。
你可以这样做:
foreach($result->style as $style) {
echo $style->passDoors . "\n";
echo $style->drivetrain . "\n";
}
如果您确定在给定示例中始终只有一种索引为 0 的样式,您也可以像这样访问它:
echo $result->style[0]->passDoors . "\n";
echo $result->style[0]->drivetrain . "\n";
推荐阅读
- boost - Yocto sumo 与 RPI3B+ 与旧 Boost
- apache-spark - 数据框中的地图 - pyspark
- soap - SOAP 端口使用非标准 SOAP 1.2 绑定
- file - 读取扩展 Asterisk 13 OpenWRT 17.04 中的文件?存储库中缺少应用程序 ReadFile()
- regex - 正则表达式:替换匹配中的字符。不管它处于什么位置
- python - 使用 Python 动态读取 JSON 数据
- azure-devops - VSTS 不断生成新的个人访问令牌
- python - 从 QLineEdit 获取输入并将其发送到另一个 python 文件进行计算
- linux - 如何删除/修改 syslogd 消息的标头?
- javascript - json.stringify 在 javascript 中返回空数组