我有一个从json返回的数组,我可以从数组的一部分访问值,但不能从数组的另一部分访问值
echo '<strong>Barcode Number:</strong> ' . $response->products[0]->barcode_number . '<br><br>';
echo '<strong>Product Name:</strong> ' . $response->products[0]->product_name . '<br><br>';
echo '<strong>Description:</strong> ' . $response->products[0]->description . '<br><br>';
echo '<strong>Description:</strong> ' . $response->stores[0]->store_name . '<br><br>';
我得到前三个罚款,但商店的最后一个罚款返回错误
条形码编号:077341125112
产品名称:定制配件89960W E-Tek丁烷火炬
说明:89960W丁烷火炬非常适合您的家庭车库或 你的车。可用于快速维修。
注意:未定义的属性:stdClass :: $存储在 C:\ xampp \ htdocs \ customs \ production \ test-barcodelookup.php在第20行
注意:试图获取非对象的属性“ store_name” C:\ xampp \ htdocs \ customs \ production \ test-barcodelookup.php在第20行
$ch = curl_init(); // Use only one cURL connection for multiple queries
$data = get_data($url, $ch);
$response = array();
$response = json_decode($data);
echo '<strong>Barcode Number:</strong> ' . $response->products[0]->barcode_number . '<br><br>';
echo '<strong>Product Name:</strong> ' . $response->products[0]->product_name . '<br><br>';
echo '<strong>Description:</strong> ' . $response->products[0]->description . '<br><br>';
echo '<strong>Description:</strong> ' . $response->stores[0]->store_name . '<br><br>';
echo '<strong>Entire Response:</strong><pre>';
print_r($response);
echo '</pre>';
function get_data($url, $ch) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
下面是返回的数组
整个回复:
stdClass Object
(
[products] => Array
(
[0] => stdClass Object
(
[barcode_number] => 077341125112
[barcode_type] => UPC
[barcode_formats] => UPC 077341125112, EAN 0077341125112
[mpn] => 0007734112511
[model] => 89960w
[asin] =>
[product_name] => Custom Accessories 89960W E-Tek Butane Torch
[title] =>
[category] => Hardware > Tools > Hardware Torches
[manufacturer] =>
[brand] => Etek
[label] =>
[author] =>
[publisher] =>
[artist] =>
[actor] =>
[director] =>
[studio] =>
[genre] =>
[audience_rating] =>
[ingredients] =>
[nutrition_facts] =>
[color] =>
[format] =>
[package_quantity] =>
[size] =>
[length] =>
[width] =>
[height] =>
[weight] =>
[release_date] =>
[description] => Butane Torch, 89960W is ideal for your home garage or your car. Can be used for quick repairs.
[features] => Array
(
)
[images] => Array
(
[0] => https://images.barcodelookup.com/3001/30014169-1.jpg
)
[stores] => Array
(
[0] => stdClass Object
(
[store_name] => Wal-Mart.com USA, LLC
[store_price] => 14.97
[product_url] => http://www.walmart.com/ip/Custom-Accessories-89960W-E-Tek-Butane-Torch/29029306
[currency_code] => USD
[currency_symbol] => $
)
[1] => stdClass Object
(
[store_name] => Jet.com
[store_price] => 14.20
[product_url] => http://jet.com/product/detail/a43035df304c4551b45f62262402f9f2
[currency_code] => USD
[currency_symbol] => $
)
)
[reviews] => Array
(
[0] => stdClass Object
(
[name] => Ken Weber
[rating] => 5
[title] => Torch Performance
[review] => I didnt know how good this torch was until I used it and its very nice for the money. The electronic ignition fires the butane evertime. Nice feel to it. Has a trigger lock down for extended usage time. GOOD PRODUCT.
[datetime] => 2015-12-29 11:27:34
)
)
)
)
)
我正在尝试访问images数组并将其回显,而stores数组中的信息将对其进行回显。我可以从产品数组中获取信息。但我不知道如何获得其他人
这就是我要实现的目标
条形码编号:077341125112
产品名称:定制配件89960W E-Tek丁烷火炬
说明:89960W丁烷火炬非常适合您的家庭车库或 你的车。可用于快速维修。
显示产品图片。
商店:
商店名称:Wal-Mart.com USA,LLC商店价格:14.97产品网址: http://www.walmart.com/ip/Custom-Accessories-89960W-E-Tek-Butane-Torch/29029306
商店名称:Jet.com商店价格:14.20产品网址: http://jet.com/product/detail/a43035df304c4551b45f62262402f9f2
答案 0 :(得分:0)
格式化数据时,它表明商店数据在每个产品下,因此您需要将其显示为...
$response->products[0]->stores[0]->store_name
您可能还需要使用foreach()
来显示所有详细信息,包括产品和每种产品的商店。
foreach ( $response->products[0]->stores as $store ) {
// Echo out the details
echo $store->store_name;
}
答案 1 :(得分:0)
步骤:我认为进行此过程的最简单方法是使用
json_decode($json, true);
这使所有内容都成为关联数组。
这是manual for the json_decode内部函数。
后退:
$response->products[0]->stores[0]->store_name