如何在PHP中访问JSON元素,如数组对象?

时间:2017-12-16 16:51:49

标签: php json woocommerce

从触发this Webhook开始,当我在电子邮件中输出时,我会收到order object,如下所示:

  

阵列{ “ID”:1154, “PARENT_ID”:0 “状态”: “未决”, “货币”: “EUR”, “版本”: “3.2.3”, “prices_include_tax”:真” DATE_CREATED “:{” 日期 “:” 2017年12月15日   15:58:42.000000" , “timezone_type”:1, “时区”: “+ 00:00”}, “DATE_MODIFIED”:{ “日期”:“2017年12月15日   15:58:42.000000" , “timezone_type”:1, “时区”: “+ 00:00”}, “discount_total”: “0”, “discount_tax”: “0”, “shipping_total”: “0”,” shipping_tax “:” 0" , “cart_tax”: “0”, “总”: “6.50”, “total_tax”: “0”, “CUSTOMER_ID”:0 “order_key”: “wc_order_5a33f1321ba43”, “计费”:{ “FIRST_NAME”: “彼得”, “姓氏”: “帕克”, “公司”: “”, “ADDRESS_1”:“贝克   STR “” address_2。 “:” 2" , “城市”: “伦敦”, “状态”: “”, “邮政编码”: “50668”, “国”: “DE”, “电子邮件”:“东西@ gmail.com “ ”手机“: ”01627423“}, ”发货“:{ ”FIRST_NAME“: ”彼得“, ”姓氏“: ”帕克“, ”公司“: ”“, ”ADDRESS_1“:” 贝克   STR “” address_2。 “:” 2" , “城市”: “伦敦”, “状态”: “”, “邮政编码”: “50668”, “国”: “DE”}, “PAYMENT_METHOD”:“鳕鱼“ ”payment_method_title“:” 北   Abholung “” 交易   _id “:” “ ”customer_ip_address“:” ... “” customer_user_agent “所示:” Mozilla / 5.0   (macintosh; intel mac os x 10_13_2)applewebkit / 537.36(khtml,like   gecko)chrome / blabla   狩猎/ 537.36" , “created_via”: “结帐”, “customer_note”: “”, “date_completed”:NULL, “date_paid”:NULL, “cart_hash”: “be97db19eba58864b9166961ce22a706”, “号码”: “1154”,“meta_data “:[{” ID “:4731,” 钥匙 “:” _ billing_title”, “值”: “1”},{ “ID”:4732, “钥匙”: “_ shipping_title”, “值”: “1”} ], “line_items”:{ “18”:{}}, “tax_lines”:[], “shipping_lines”:{ “19”:{}}, “fee_lines”:[], “coupon_lines”:[]} < / p>

请您指出可以访问此JSON的元素,以便我可以构建来自$order对象的某些部分的消息吗?我尝试$data = json_decode($order)$data->date_created结合使用,但是当我丢弃最后一部分时,我没有给我任何东西。

2 个答案:

答案 0 :(得分:3)

它是json,除了开始时的Array部分(假设你试图访问date_created):

您将使用json_decode(),但date_created也是一个数组,因此您还需要从中选择所需的项目。

$data = json_decode($order, true);

echo $data['date_created']['date']; // 2017-12-15 15:58:42.000000

因此,如果您想要客户名称,例如:

echo $data['billing']['first_name'].' '.$data['billing']['last_name'];

通过print_r($data),你可以看到它的结构。

Array
(
    [id] => 1154
    [parent_id] => 0
    [status] => pending
    [currency] => EUR
    [version] => 3.2.3
    [prices_include_tax] => 1
    [date_created] => Array
        (
            [date] => 2017-12-15 15:58:42.000000
            [timezone_type] => 1
            [timezone] => +00:00
        )

    [date_modified] => Array
        (
            [date] => 2017-12-15 15:58:42.000000
            [timezone_type] => 1
            [timezone] => +00:00
        )

    [discount_total] => 0
    [discount_tax] => 0
    [shipping_total] => 0
    [shipping_tax] => 0
    [cart_tax] => 0
    [total] => 6.50
    [total_tax] => 0
    [customer_id] => 0
    [order_key] => wc_order_5a33f1321ba43
    [billing] => Array
        (
            [first_name] => Peter
            [last_name] => Parker
            [company] => 
            [address_1] => Baker Str.
            [address_2] => 2
            [city] => London
            [state] => 
            [postcode] => 50668
            [country] => DE
            [email] => something@gmail.com
            [phone] => 01627423
        )

    [shipping] => Array
        (
            [first_name] => Peter
            [last_name] => Parker
            [company] => 
            [address_1] => Baker Str.
            [address_2] => 2
            [city] => London
            [state] => 
            [postcode] => 50668
            [country] => DE
        )

    [payment_method] => cod
    [payment_method_title] => Bei Abholung
    [transaction _id] => 
    [customer_ip_address] => ...
    [customer_user_agent] => mozilla/5.0 (macintosh; intel mac os x 10_13_2) applewebkit/537.36 (khtml, like gecko) chrome/blabla safari/537.36
    [created_via] => checkout
    [customer_note] => 
    [date_completed] => 
    [date_paid] => 
    [cart_hash] => be97db19eba58864b9166961ce22a706
    [number] => 1154
    [meta_data] => Array
        (
            [0] => Array
                (
                    [id] => 4731
                    [key] => _billing_title
                    [value] => 1
                )

            [1] => Array
                (
                    [id] => 4732
                    [key] => _shipping_title
                    [value] => 1
                )

        )

    [line_items] => Array
        (
            [18] => Array
                (
                )

        )

    [tax_lines] => Array
        (
        )

    [shipping_lines] => Array
        (
            [19] => Array
                (
                )

        )

    [fee_lines] => Array
        (
        )

    [coupon_lines] => Array
        (
        )

)

如果你想要一个简单的输出,你可以使用递归函数遍历数组,这将生成你的电子邮件。

echo '<h2>Order Details</h2>'.PHP_EOL;
echo order_details(json_decode($json, true));

function order_details($items, $str = null) {   
    foreach ($items as $key => $item) {
        if (is_array($item)) {
            $str .= PHP_EOL.'<h3>'.ucwords(str_replace('_', ' ', $key)).'</h3>'.PHP_EOL;
            $str .= order_details($item, $str);
        } else {
            $str .= '<b>'.ucwords(str_replace('_', ' ', $key)).'</b>: '.$item.'</br>'.PHP_EOL;
        }
    }
    return $str;
}

其输出方式如下:https://3v4l.org/l9BS4

<h2>Order Details</h2>
<b>Id</b>: 1154</br>
<b>Parent Id</b>: 0</br>
<b>Status</b>: pending</br>
<b>Currency</b>: EUR</br>
<b>Version</b>: 3.2.3</br>
<b>Prices Include Tax</b>: 1</br>

<h3>Date Created</h3>
<b>Date</b>: 2017-12-15 15:58:42.000000</br>
<b>Timezone Type</b>: 1</br>
<b>Timezone</b>: +00:00</br>

<h3>Date Modified</h3>
<b>Date</b>: 2017-12-15 15:58:42.000000</br>
<b>Timezone Type</b>: 1</br>
<b>Timezone</b>: +00:00</br>
<b>Discount Total</b>: 0</br>
<b>Discount Tax</b>: 0</br>
<b>Shipping Total</b>: 0</br>
<b>Shipping Tax</b>: 0</br>
<b>Cart Tax</b>: 0</br>
<b>Total</b>: 6.50</br>
<b>Total Tax</b>: 0</br>
<b>Customer Id</b>: 0</br>
<b>Order Key</b>: wc_order_5a33f1321ba43</br>

<h3>Billing</h3>
<b>First Name</b>: Peter</br>
<b>Last Name</b>: Parker</br>
<b>Company</b>: </br>
<b>Address 1</b>: Baker Str.</br>
<b>Address 2</b>: 2</br>
<b>City</b>: London</br>
<b>State</b>: </br>
<b>Postcode</b>: 50668</br>
<b>Country</b>: DE</br>
<b>Email</b>: something@gmail.com</br>
<b>Phone</b>: 01627423</br>

<h3>Shipping</h3>
<b>First Name</b>: Peter</br>
<b>Last Name</b>: Parker</br>
<b>Company</b>: </br>
<b>Address 1</b>: Baker Str.</br>
<b>Address 2</b>: 2</br>
<b>City</b>: London</br>
<b>State</b>: </br>
<b>Postcode</b>: 50668</br>
<b>Country</b>: DE</br>
<b>Payment Method</b>: cod</br>
<b>Payment Method Title</b>: Bei Abholung</br>
<b>Transaction  Id</b>: </br>
<b>Customer Ip Address</b>: ...</br>
<b>Customer User Agent</b>: mozilla/5.0 (macintosh; intel mac os x 10_13_2) applewebkit/537.36 (khtml, like gecko) chrome/blabla safari/537.36</br>
<b>Created Via</b>: checkout</br>
<b>Customer Note</b>: </br>
<b>Date Completed</b>: </br>
<b>Date Paid</b>: </br>
<b>Cart Hash</b>: be97db19eba58864b9166961ce22a706</br>
<b>Number</b>: 1154</br>

<h3>Meta Data</h3>

<h3>0</h3>
<b>Id</b>: 4731</br>
<b>Key</b>: _billing_title</br>
<b>Value</b>: 1</br>

<h3>1</h3>
<b>Id</b>: 4732</br>
<b>Key</b>: _shipping_title</br>
<b>Value</b>: 1</br>

<h3>Line Items</h3>

<h3>18</h3>

<h3>Tax Lines</h3>

<h3>Shipping Lines</h3>

<h3>19</h3>

<h3>Fee Lines</h3>

<h3>Coupon Lines</h3>

答案 1 :(得分:1)

使用时

$data = json_decode($order);

它将创建一个对象,将此对象转换为您只需传递参数的关联数组

$data = json_decode($order, true);