【发布时间】:2019-10-13 00:32:53
【问题描述】:
我目前正在从 API 请求信息。当我尝试使用 json_decode() 解码 json 的响应时,它没有给我正确的数据。当我使用var_dump() 时,它确实如此。如何让我的代码正确显示 API 数据?
我已经尝试在互联网上查找。我已经成功使用了var_dump(),但它没有像dd() 那样正确显示信息。
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$json = json_decode($response);
}
这是我用来访问 API 数据的代码。这是 var_dump($json) 的响应:
Example: array(344) {
[0]=>
object(stdClass)#1148 (1) {
["product"]=>
object(stdClass)#1141 (3) {
["information"]=>
object(stdClass)#1140 (1) {
["deliveryDays"]=>
string(2) "10"
}
["attributes"]=>
array(6) {
[0]=>
object(stdClass)#1142 (2) {
["attribute"]=>
string(18) "Type of sunglasses"
["value"]=>
string(5) "basic"
}
[1]=>
object(stdClass)#1143 (2) {
["attribute"]=>
string(15) "variantid (mcp)"
["value"]=>
string(9) "P105675W1"
}
[2]=>
object(stdClass)#1144 (2) {
["attribute"]=>
string(11) "color (mcp)"
["value"]=>
string(17) "Solid Black/White"
}
[3]=>
object(stdClass)#1145 (2) {
["attribute"]=>
string(8) "quantity"
["value"]=>
string(3) "100"
}
[4]=>
object(stdClass)#1146 (2) {
["attribute"]=>
string(16) "printing process"
["value"]=>
string(12) "tampon print"
}
[5]=>
object(stdClass)#1147 (2) {
["attribute"]=>
string(13) "delivery type"
["value"]=>
string(6) "normal"
}
}
["price"]=>
float(213.3)
}
}
当我使用dd($json):
+"product": {#1150 ▼
+"information": {#1149 ▶}
+"attributes": array:6 [▼
0 => {#1151 …2}
1 => {#1152 …2}
2 => {#1153 …2}
3 => {#1154 …2}
4 => {#1155 …2}
5 => {#1156 …2}
]
+"price": 407.7
}
如您所见,API 在数组 6 中显示 ...2 但它应该显示一个值。我的代码有什么问题?
我希望 API 正确地 dd 数据并向我显示 api 中的数据
【问题讨论】:
标签: php arrays laravel api laravel-5