这是我的PHP代码:
<?php
require_once 'lib/shopify.php';
//require_once 'csv.php';
$t="bac2486aa2b31aa5aed1fdd62e77a4ae";
$sc = new ShopifyClient("mine-329.myshopify.com/", $t, API_KEY, SECRET);
if(!isset($t))
{
if(!isset($_GET['signature']))
{
$url = $sc->getAuthorizeUrl("read_orders");
header('Location: '.$url);
}
if(isset($_GET['code']))
{
$accTok = $sc->getAccessToken($_GET['code']);
echo "token=".$accTok;exit;
$orders = $sc->call('GET', 'admin/orders.json', array('published_status'=>'published'));
foreach($orders as $order) {
echo $order['id'];
echo $order['email'];
}
}
}
if(isset($t))
{
$orders = $sc->call('GET', 'admin/orders.json', array('published_status'=>'published'));
print_r($orders);
}
?>
这是我的输出:
阵
(
[0] =&gt;排列
(
[order_number] =&gt; 1005
[discount_codes] =&gt;排列
(
)
[note_attributes] => Array
(
)
[tax_lines] => Array
(
[0] => Array
(
[price] => 175.00
[rate] => 0.125
[title] => VAT
) )
[tags] =>
[line_items] => Array
(
[0] => Array
(
[title] => Carnation mini - Alibi
[variant_id] => 700039407
[variant_title] =>
[vendor] => mine
[name] => Carnation mini - Alibi
[variant_inventory_management] => shopify
[properties] => Array
(
)
[product_exists] => 1
[fulfillable_quantity] => 0
[tax_lines] => Array
(
[0] => Array
(
[price] => 75.00
[rate] => 0.125
[title] => VAT
) ))
[1] => Array
(
[title] => Calla lily white
[variant_id] => 700039091
[variant_title] =>
[vendor] => mine
[name] => Calla lily white
[variant_inventory_management] => shopify
[properties] => Array
(
)
[product_exists] => 1
[fulfillable_quantity] => 0
[tax_lines] => Array
(
[0] => Array
(
[price] => 100.00
[rate] => 0.125
[title] => VAT
) ) ) )
[shipping_lines] => Array
(
[0] => Array
(
[code] => Standard Shipping
[price] => 8.00
[source] => shopify
[title] => Standard Shipping
[tax_lines] => Array
(
[0] => Array
(
[price] => 0.00
[rate] => 0.125
[title] => VAT
) ) ) )
[payment_details] => Array
(
[avs_result_code] =>
[credit_card_bin] => 1
[cvv_result_code] =>
[credit_card_number] => •••• •••• •••• 1
[credit_card_company] => Bogus
)
[billing_address] => Array
(
[address1] => 4th
[address2] =>
[city] => c
[company] =>
[country] => India
[first_name] => selvarani
[last_name] => raja
[latitude] => 21.504394
[longitude] => 82.71767
[phone] => 919566552885
[province] => Tamil Nadu
[zip] => 6000014
[name] => selvarani raja
[country_code] => IN
[province_code] => TN
)
[shipping_address] => Array
(
[country_code] => IN
[province_code] => TN
)
[fulfillments] => Array
(
[0] => Array
(
[tracking_numbers] => Array
(
)
[tracking_url] =>
[tracking_urls] => Array
(
)
[receipt] => Array
(
)
[line_items] => Array
(
[0] => Array
(
[title] => Carnation mini - Alibi
[variant_id] => 700039407
[variant_title] =>
[vendor] => mine
[name] => Carnation mini - Alibi
[variant_inventory_management] => shopify
[properties] => Array
(
)
[product_exists] => 1
[fulfillable_quantity] => 0
[tax_lines] => Array
(
[0] => Array
(
[price] => 75.00
[rate] => 0.125
[title] => VAT
) ) )
[1] => Array
(
[variant_id] => 700039091
[name] => Calla lily white
[variant_inventory_management] => shopify
[properties] => Array
(
)
[product_exists] => 1
[fulfillable_quantity] => 0
[tax_lines] => Array
(
[0] => Array
(
[price] => 100.00
[rate] => 0.125
[title] => VAT
) ) ) ) ) )
[refunds] =&gt;排列 ( )
[customer] => Array
(
[last_order_name] =>
[default_address] => Array
(
[province_code] => TN
[country_code] => IN
[default] => 1
) ) ) )
现在我想得到产品名称,即Carnation mini - Alibi和Calla lily white。
如果我把:
foreach($orders as order) {
echo $order['line_items'][0]['name'];
它将显示解析错误。谁能指导我?
答案 0 :(得分:1)
注意完整输出的数组$orders
然后找到在实现目标元素时应该传递多少个数组
然后根据该序列添加foreach循环的数量,并等于传递的数组的数量。
以下是一个例子: 假设你有:
`$orders = array( [0] => array( [0] => array( ['name'] = "some value for name" ) ) )`
提示:括号的数量等于数组的数量,然后是foreach循环的数量。
foreach($orders as $order) {
foreach($order as $items){
foreach($items as $item){
echo $item['name'];
}
}
}
答案 1 :(得分:0)
至少应该是:
foreach($orders as $order) {
echo $order['line_items'][0]['name'];
}
(注意foreach构造的美元符号。)
答案 2 :(得分:0)
好的,再试一次,让我们轻松阅读:
$orders2 = $orders[0]['line_items'];
$i = 0;
foreach($orders2 as $order) {
echo $order[$i]['name'];
$i++;
}