php 遍历 stdClass 对象和数组
php looping through stdClass objects and arrays
嗨,我使用 woocommerce rest API 访问在 woocommerce 网站上制作的订单信息。
数据返回为 json,我可以使用此循环访问所有数据:
$orders = $connect->orders->get();
$order_numbers = $orders->orders;
foreach ($order_numbers as $order_number) {
$order_data = $connect->orders->get($order_number->order_number);
echo '</br>' . $order_number->order_number . '</br>';
echo $order_data->order->total . '</br>';
$line_items = $order_data->order->line_items;
foreach($line_items as $item){
echo $item->name . ' x ' . $item->quantity . '</br>';
}
}
第一行是 API 获取订单列表的调用。
这使我可以在不指定 order_number 的情况下从第一个对象输出数据。但是,当访问 stdObject 中的数组时,例如 'line_items' 我必须定义一个订单号。为了克服这个问题,这次我在 foreach 内部进行了另一个 api 调用,指定订单号以访问信息。
$order_data = $connect->orders->get($order_number->order_number);
问题是第二次 API 调用会大大降低整个页面的速度。
我有没有办法将其减少到单个 api 调用以加速页面呈现。
作为参考,这是我正在访问的数据:
object(stdClass)[15]
public 'order' =>
object(stdClass)[16]
public 'id' => int 22
public 'order_number' => int 22
public 'created_at' => string '2015-07-30T14:01:54Z' (length=20)
public 'updated_at' => string '2015-07-30T14:01:54Z' (length=20)
public 'completed_at' => string '2015-07-30T13:01:54Z' (length=20)
public 'status' => string 'on-hold' (length=7)
public 'currency' => string 'GBP' (length=3)
public 'total' => string '3.84' (length=4)
public 'subtotal' => string '3.84' (length=4)
public 'total_line_items_quantity' => int 2
public 'total_tax' => string '0.00' (length=4)
public 'total_shipping' => string '0.00' (length=4)
public 'cart_tax' => string '0.00' (length=4)
public 'shipping_tax' => string '0.00' (length=4)
public 'total_discount' => string '0.00' (length=4)
public 'shipping_methods' => string '' (length=0)
public 'payment_details' =>
object(stdClass)[17]
public 'method_id' => string 'bacs' (length=4)
public 'method_title' => string 'Direct Bank Transfer' (length=20)
public 'paid' => boolean false
public 'billing_address' =>
object(stdClass)[18]
public 'first_name' => string 'Chris' (length=5)
public 'last_name' => string '#' (length=5)
public 'company' => string '' (length=0)
public 'address_1' => string '#' (length=4)
public 'address_2' => string '' (length=0)
public 'city' => string '#' (length=7)
public 'state' => string '' (length=0)
public 'postcode' => string '#' (length=7)
public 'country' => string 'GB' (length=2)
public 'email' => string '#' (length=20)
public 'phone' => string '#' (length=11)
public 'shipping_address' =>
object(stdClass)[19]
public 'first_name' => string 'Chris' (length=5)
public 'last_name' => string '#' (length=5)
public 'company' => string '' (length=0)
public 'address_1' => string '#' (length=4)
public 'address_2' => string '' (length=0)
public 'city' => string '#' (length=7)
public 'state' => string '' (length=0)
public 'postcode' => string '#' (length=7)
public 'country' => string 'GB' (length=2)
public 'note' => string '' (length=0)
public 'customer_ip' => string '#' (length=15)
public 'customer_user_agent' => string 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4' (length=134)
public 'customer_id' => int 1
public 'view_order_url' => string '#' (length=58)
public 'line_items' =>
array (size=2)
0 =>
object(stdClass)[20]
...
1 =>
object(stdClass)[21]
...
public 'shipping_lines' =>
array (size=0)
empty
根据他们的规范 (http://woothemes.github.io/woocommerce-rest-api-docs/#view-list-of-orders),您的 $orders 结果中应该已经包含了所有信息。
嗨,我使用 woocommerce rest API 访问在 woocommerce 网站上制作的订单信息。
数据返回为 json,我可以使用此循环访问所有数据:
$orders = $connect->orders->get();
$order_numbers = $orders->orders;
foreach ($order_numbers as $order_number) {
$order_data = $connect->orders->get($order_number->order_number);
echo '</br>' . $order_number->order_number . '</br>';
echo $order_data->order->total . '</br>';
$line_items = $order_data->order->line_items;
foreach($line_items as $item){
echo $item->name . ' x ' . $item->quantity . '</br>';
}
}
第一行是 API 获取订单列表的调用。
这使我可以在不指定 order_number 的情况下从第一个对象输出数据。但是,当访问 stdObject 中的数组时,例如 'line_items' 我必须定义一个订单号。为了克服这个问题,这次我在 foreach 内部进行了另一个 api 调用,指定订单号以访问信息。
$order_data = $connect->orders->get($order_number->order_number);
问题是第二次 API 调用会大大降低整个页面的速度。
我有没有办法将其减少到单个 api 调用以加速页面呈现。
作为参考,这是我正在访问的数据:
object(stdClass)[15]
public 'order' =>
object(stdClass)[16]
public 'id' => int 22
public 'order_number' => int 22
public 'created_at' => string '2015-07-30T14:01:54Z' (length=20)
public 'updated_at' => string '2015-07-30T14:01:54Z' (length=20)
public 'completed_at' => string '2015-07-30T13:01:54Z' (length=20)
public 'status' => string 'on-hold' (length=7)
public 'currency' => string 'GBP' (length=3)
public 'total' => string '3.84' (length=4)
public 'subtotal' => string '3.84' (length=4)
public 'total_line_items_quantity' => int 2
public 'total_tax' => string '0.00' (length=4)
public 'total_shipping' => string '0.00' (length=4)
public 'cart_tax' => string '0.00' (length=4)
public 'shipping_tax' => string '0.00' (length=4)
public 'total_discount' => string '0.00' (length=4)
public 'shipping_methods' => string '' (length=0)
public 'payment_details' =>
object(stdClass)[17]
public 'method_id' => string 'bacs' (length=4)
public 'method_title' => string 'Direct Bank Transfer' (length=20)
public 'paid' => boolean false
public 'billing_address' =>
object(stdClass)[18]
public 'first_name' => string 'Chris' (length=5)
public 'last_name' => string '#' (length=5)
public 'company' => string '' (length=0)
public 'address_1' => string '#' (length=4)
public 'address_2' => string '' (length=0)
public 'city' => string '#' (length=7)
public 'state' => string '' (length=0)
public 'postcode' => string '#' (length=7)
public 'country' => string 'GB' (length=2)
public 'email' => string '#' (length=20)
public 'phone' => string '#' (length=11)
public 'shipping_address' =>
object(stdClass)[19]
public 'first_name' => string 'Chris' (length=5)
public 'last_name' => string '#' (length=5)
public 'company' => string '' (length=0)
public 'address_1' => string '#' (length=4)
public 'address_2' => string '' (length=0)
public 'city' => string '#' (length=7)
public 'state' => string '' (length=0)
public 'postcode' => string '#' (length=7)
public 'country' => string 'GB' (length=2)
public 'note' => string '' (length=0)
public 'customer_ip' => string '#' (length=15)
public 'customer_user_agent' => string 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4' (length=134)
public 'customer_id' => int 1
public 'view_order_url' => string '#' (length=58)
public 'line_items' =>
array (size=2)
0 =>
object(stdClass)[20]
...
1 =>
object(stdClass)[21]
...
public 'shipping_lines' =>
array (size=0)
empty
根据他们的规范 (http://woothemes.github.io/woocommerce-rest-api-docs/#view-list-of-orders),您的 $orders 结果中应该已经包含了所有信息。