Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP/Wordpress中获取数组项_Php_Arrays_Wordpress_Object - Fatal编程技术网

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

如何在PHP/Wordpress中获取数组项,php,arrays,wordpress,object,Php,Arrays,Wordpress,Object,嗨,我有下面的代码从WooCommerce获取运输方法 global $woocommerce; $cart = wc()->cart->get_shipping_packages(); $sm = wc()->shipping->calculate_shipping_for_package($cart); return $sm; 结果将返回如下数组: 如何让它从速率数组中获取数据?我想要的值是id、

嗨,我有下面的代码从WooCommerce获取运输方法

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

        return $sm;
结果将返回如下数组:

如何让它从速率数组中获取数据?我想要的值是id、label和method\u id

印刷费($sm);退出


它看起来像一个JSON对象,用户需要转换JSON_decode()函数。访问下面的链接了解更多信息

这是JSON对象吗?是的。它将返回为json,如图所示。我希望它像id=>$value->id、label=>$value->label、method\u id=>$value->method\u id一样重新运行。显然,我在数组和多循环方面非常差,请帮助:)你所拥有的不是数组。是数组的javascript字符串表示形式,因此必须按照@parag的建议使用json_decode将其转换回PHP。erm我尝试添加
返回数组(id=>$value->id,label=>$value->label,method_id=>$value->method_id)
但返回null,图中有5种配送方式。是否缺少某些内容?让我更新将返回所有methodserm的代码,现在它只返回我方括号[]空数据@MitulYou能否将jsonstring而不是image,以便我复制并检查。完成添加字符串
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
                )

        )

)
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;