php循环多维数组woocommerce REST API

I am using the PHP library for the woocommerce REST API. I need to gather information regarding orders. I have tried many different ways of looping over the array that is returned from the get->orders function but cannot retrieve all the information I need.

Here is a var_dump of the returned array:

enter image description here

I can return the order number, order total, total items and date/time order created.

But I want to list out the items from the order, these items are in the 'line_items' array within the main array. The image doesn't show the items but it would be the same method to return the customer information from the 'billing_address' array again within the main array.

The current loop I have is this:

$orders = $connect->orders->get();



            foreach( $orders as $order ) {
              foreach( $order as $value ) {

                    echo $value["id"];
                    echo $value["total"];
                    echo $value["total_line_items_quantity"];
                    echo $value["status"];

              }
            }

Appreciate any help.

Note: here is the woocommerce REST API: http://woothemes.github.io/woocommerce-rest-api-docs/#view-list-of-orders

In response to Mihia

[shipping_address] => Array
                    (
                        [first_name] => Chris
                        [last_name] => data
                        [company] => 
                        [address_1] => data
                        [address_2] => 
                        [city] => data
                        [state] => 
                        [postcode] => data
                        [country] => GB
                    )

                [note] => 
                [customer_ip] => IP address
                [customer_user_agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240
                [customer_id] => 1
                [view_order_url] => data                
[line_items] => Array
                    (
                        [0] => Array
                            (
                                [id] => 4
                                [subtotal] => 0.00
                                [subtotal_tax] => 0.00
                                [total] => 0.00
                                [total_tax] => 0.00
                                [price] => 0.00
                                [quantity] => 1
                                [tax_class] => 
                                [name] => Chips (Regular)
                                [product_id] => 8
                                [sku] => sku_8
                                [meta] => Array
                                    (
                                    )

                            )

                        [1] => Array
                            (
                                [id] => 5
                                [subtotal] => 0.00
                                [subtotal_tax] => 0.00
                                [total] => 0.00
                                [total_tax] => 0.00
                                [price] => 0.00
                                [quantity] => 1
                                [tax_class] => 
                                [name] => Cod (Regular)
                                [product_id] => 14
                                [sku] => cod01
                                [meta] => Array
                                    (
                                    )

                            )

                    )

                [shipping_lines] => Array
                    (
                    )

                [tax_lines] => Array
                    (
                    )

                [fee_lines] => Array
                    (
                    )

                [coupon_lines] => Array
                    (
                    )

                [customer] => Array
                    (
                        [id] => 1
                        [created_at] => 2015-07-29T16:12:13Z
                        [email] => data
                        [first_name] => 
                        [last_name] => 
                        [username] => data
                        [role] => data
                        [last_order_id] => 22
                        [last_order_date] => 2015-07-30T14:01:54Z
                        [orders_count] => 5
                        [total_spent] => 7.96
                        [avatar_url] => http://2.gravatar.com/avatar/?s=96
                        [billing_address] => Array
                            (
                                [first_name] => Chris
                                [last_name] => data
                                [company] => 
                                [address_1] => data
                                [address_2] => 
                                [city] => data
                                [state] => 
                                [postcode] => data
                                [country] => GB
                                [email] => data
                                [phone] => data
                            )

                        [shipping_address] => Array
                            (
                                [first_name] => Chris
                                [last_name] => data
                                [company] => 
                                [address_1] => data
                                [address_2] => 
                                [city] => data
                                [state] => 
                                [postcode] => data
                                [country] => data
                            )

                    )

            )

        [8] => Array
            (
                [id] => 13
                [order_number] => 13
                [created_at] => 2015-07-29T17:04:02Z
                [updated_at] => 2015-07-29T17:04:02Z
                [completed_at] => 2015-07-29T16:04:02Z
                [status] => processing
                [currency] => GBP
                [total] => 0.00
                [subtotal] => 0.00
                [total_line_items_quantity] => 1
                [total_tax] => 0.00
                [total_shipping] => 0.00
                [cart_tax] => 0.00
                [shipping_tax] => 0.00
                [total_discount] => 0.00
                [shipping_methods] => 
                [payment_details] => Array
                    (
                        [method_id] => 
                        [method_title] => 
                        [paid] => 1
                    )

                [billing_address] => Array
                    (
                        [first_name] => Chris
                        [last_name] => data
                        [company] => 
                        [address_1] => data
                        [address_2] => 
                        [city] => data
                        [state] => 
                        [postcode] => data
                        [country] => GB
                        [email] => data
                        [phone] => data
                    )

                [shipping_address] => Array
                    (
                        [first_name] => Chris
                        [last_name] => data
                        [company] => 
                        [address_1] => data
                        [address_2] => 
                        [city] => data
                        [state] => 
                        [postcode] => data
                        [country] => GB
                    )

                [note] => 
                [customer_ip] => data
                [customer_user_agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240
                [customer_id] => 1
                [view_order_url] => data                
[line_items] => Array
                    (
                        [0] => Array
                            (
                                [id] => 3
                                [subtotal] => 0.00
                                [subtotal_tax] => 0.00
                                [total] => 0.00
                                [total_tax] => 0.00
                                [price] => 0.00
                                [quantity] => 1
                                [tax_class] => 
                                [name] => Chips (Regular)
                                [product_id] => 8
                                [sku] => sku_8
                                [meta] => Array
                                    (
                                    )

                            )

                    )

                [shipping_lines] => Array
                    (
                    )

                [tax_lines] => Array
                    (
                    )

                [fee_lines] => Array
                    (
                    )

                [coupon_lines] => Array
                    (
                    )

                [customer] => Array
                    (
                        [id] => 1
                        [created_at] => 2015-07-29T16:12:13Z
                        [email] => data
                        [first_name] => 
                        [last_name] => 
                        [username] => data
                        [role] => data
                        [last_order_id] => 22
                        [last_order_date] => 2015-07-30T14:01:54Z
                        [orders_count] => 5
                        [total_spent] => 7.96
                        [avatar_url] => http://1.gravatar.com/avatar/?s=96
                        [billing_address] => Array
                            (
                                [first_name] => Chris
                                [last_name] => data
                                [company] => 
                                [address_1] => data
                                [address_2] => 
                                [city] => data
                                [state] => 
                                [postcode] => data
                                [country] => GB
                                [email] => data
                                [phone] => data
                            )

                        [shipping_address] => Array
                            (
                                [first_name] => Chris
                                [last_name] => data
                                [company] => 
                                [address_1] => data
                                [address_2] => 
                                [city] => data
                                [state] => 
                                [postcode] => data
                                [country] => GB
                            )

                    )

            )

So, assuming that $orders holds the array in your image, your line items would be available as follows:

$line_items = $orders['order']['line_items'];

I don't see any use case for using foreach loop or similar iterative traversal of $orders as you have a very specifically-structured set of nested associative arrays.

For example foreach( $orders as $order ) makes no sense as the first iteration would give you the order information, whereas the second iteration would go into the http request information. These are not like data structures so this sort of loop makes no sense.

Similarly, looping $order makes no sense either. Even assuming we are on the first iteration of the outer loop, which gives if the order information, this inner loop would simply loop over the values of the array with keys not even being looked at, so you would get values like:

21
21
'2015-07-30T13:09:31Z'
...
Array()
...

You would eventually encounter your line_items array, but you wouldn't know when you did, because you have lost reference to the key associated with the value.