从 PHP 网络服务输出中形成变量
Forming variables out of a PHP web service output
我正在尝试根据从 Web 服务接收到的输出形成变量。在将车辆的 VIN 输入系统后,Web 服务会提供有关车辆的详细信息。以下是输入 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
和 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"
非常感谢您的意见。
虽然 style
是一个数组,但您应该将其作为数组处理。
似乎可以传递多种样式。
所以没有一个传动系统而是多个传动系统。
你可以这样做:
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";
我正在尝试根据从 Web 服务接收到的输出形成变量。在将车辆的 VIN 输入系统后,Web 服务会提供有关车辆的详细信息。以下是输入 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
和 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"
非常感谢您的意见。
虽然 style
是一个数组,但您应该将其作为数组处理。
似乎可以传递多种样式。
所以没有一个传动系统而是多个传动系统。
你可以这样做:
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";