如何在PHP/Wordpress中获取数组项

1 php arrays wordpress object

嗨,我有以下代码来获取WooCommerce的送货方式.

        global $woocommerce;
        $cart = wc()->cart->get_shipping_packages();
        $sm =  wc()->shipping->calculate_shipping_for_package($cart);

        return $sm;
Run Code Online (Sandbox Code Playgroud)

结果将返回一个这样的数组:

在此输入图像描述

如何从速率数组中获取数据?我想要的值是id,label和method_id?

的print_r($ SM); 出口;

Array
(
    [0] => Array
        (
            [contents] => Array
                (
                )

            [contents_cost] => 0
            [applied_coupons] => Array
                (
                )

            [user] => Array
                (
                    [ID] => 1
                )

            [destination] => Array
                (
                    [country] => SG
                    [state] => 
                    [postcode] => 
                    [city] => 
                    [address] => 
                    [address_2] => 
                )

        )

    [rates] => Array
        (
            [international_delivery] => WC_Shipping_Rate Object
                (
                    [id] => international_delivery
                    [label] => International Flat Rate
                    [cost] => 10
                    [taxes] => Array
                        (
                        )

                    [method_id] => international_delivery
                )

            [table_rate-5 : 7] => WC_Shipping_Rate Object
                (
                    [id] => table_rate-5 : 7
                    [label] => Registered Mail
                    [cost] => 0
                    [taxes] => Array
                        (
                        )

                    [method_id] => table_rate
                )

            [table_rate-5 : 8] => WC_Shipping_Rate Object
                (
                    [id] => table_rate-5 : 8
                    [label] => Non-Registered Mail
                    [cost] => 0
                    [taxes] => Array
                        (
                        )

                    [method_id] => table_rate
                )

            [table_rate-5 : 9] => WC_Shipping_Rate Object
                (
                    [id] => table_rate-5 : 9
                    [label] => Courier
                    [cost] => 0
                    [taxes] => Array
                        (
                        )

                    [method_id] => table_rate
                )

        )

)
Run Code Online (Sandbox Code Playgroud)

Mit*_*tul 5

foreach ($sm['rates'] as $key => $value)
{
    $method = array();
    $method['label'] = $value->label;
    $method['method'] = $value->method_id;
    $method['id'] = $value->id;
    $shippingMethods[] = $method;
}
return $shippingMethods;
Run Code Online (Sandbox Code Playgroud)