【问题标题】:How to get array items in PHP/Wordpress如何在 PHP/Wordpress 中获取数组项
【发布时间】:2016-02-03 06:29:53
【问题描述】:

您好,我有以下代码可以从 WooCommerce 获取运输方式。

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

        return $sm;

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

我如何使用它从速率数组中获取数据?而我想要的值是 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
                )

        )

)

【问题讨论】:

  • 这是一个 JSON 对象吗?
  • 是的。它将以 json 格式返回,如图所示。我希望它像 id=>$value->id , label => $value->label , method_id=>$value->method_id 这样重新调整。显然我在数组和多循环方面非常糟糕,请帮忙:)
  • 你所拥有的不是数组。是数组的 javascript 字符串表示形式,因此您必须按照 @parag 的建议使用 json_decode 将其转换回 PHP。

标签: php arrays wordpress object


【解决方案1】:
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;

【讨论】:

  • erm 我尝试添加 return array(id=>$value->id , label => $value->label , method_id=>$value->method_id); 但它返回 null 并且在图片中有 5 种运输方式。有什么遗漏吗?
  • 让我更新将返回所有方法的代码
  • erm 现在它只返回我括号 [] 空数据 @Mitul
  • 你能不能把 jsonstring 代替 image 让我复制和检查。
  • 完成添加字符串
【解决方案2】:

它看起来像一个 JSON 对象,用户 json_decode() 函数来转换。访问下面的链接以获取更多信息http://php.net/manual/en/function.json-decode.php

【讨论】:

    猜你喜欢
    • 2013-01-19
    • 1970-01-01
    • 2020-12-30
    • 2016-02-14
    • 2015-02-23
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    相关资源
    最近更新 更多