在PHP中获取数组元素的正确方法

问题描述 投票:-2回答:1

以下是推送到我的webhook的数组数据示例

我对PHP不太了解并知道我正在调用返回的电子邮件错误,我如何从该子数组中获取电子邮件地址?我想也许可能超过1个电子邮件的阵列父母,但我想现在很好只是抓住电子邮件结果,如果存在。

PHP:

//Get and decode JSON
$str = file_get_contents("php://input");
$json = json_decode($str, true);
// debugging
print_r($json);

$tracking_number = $json['msg']['tracking_number'];
$tracking_status = $json['msg']['tag'];
$tracking_email = $json['msg']['emails'];
$customer_name = $json['msg']['customer_name'];

$userarr = Array(
  Array(
    'tracking_number'=>$tracking_number,
    'tracking_status'=>$tracking_status,
    'tracking_email'=>$tracking_email,
    'customer_name'=>$customer_name
  )
);

// debugging
print_r($userarr);

//Update to the latest shipment status
//update_post_meta( $shiporderid, '_shipment_status', $shipmentstatus );
//Check if shipping status =  InTransit, set order status to complete
//if($shipmentstatus == 'InTransit'):
//  $order = wc_get_order( $shiporderid );
//  $order->update_status( 'completed' );
//endif;

$m = new MailSender;
$m->sendMail($userarr, 'StatusUpdate');

返回的阵列:

{
    "event": "tracking_update",
    "msg": {
        "id": "5a288fdea631d07e0b0e410d",
        "tracking_number": "EK329287025GB",
        "title": "EK329287025GB",
        "origin_country_iso3": "GBR",
        "destination_country_iso3": null,
        "shipment_package_count": 1,
        "active": false,
        "order_id": null,
        "order_id_path": null,
        "customer_name": "James Brandon",
        "source": "web",
        "emails": [
            "[email protected]"
        ],
        "smses": [],
        "custom_fields": {},
        "tag": "Delivered",
        "tracked_count": 1,
        "expected_delivery": null,
        "signed_by": null,
        "shipment_type": null,
        "created_at": "2017-12-07T00:48:30+00:00",
        "updated_at": "2017-12-07T00:48:37+00:00",
        "slug": "parcel-force",
        "unique_token": "r_30v1xzI-G",
        "path": "r_30v1xzI-G",
        "shipment_weight": null,
        "shipment_weight_unit": null,
        "content_type": "json",
        "delivery_time": 5,
        "checkpoints": [
            {
                "location": "Gatwick Depot",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "On route to hub",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-11-27T17:26:00",
                "slug": "parcel-force"
            },
            {
                "location": "International Hub",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Sorted for export",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-11-27T23:13:00",
                "slug": "parcel-force"
            },
            {
                "location": "International Hub",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Exported from the UK",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-11-27T23:18:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Tracked in destination country",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-11-29T02:46:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Tracked in destination country",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-11-30T06:07:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Prepared for delivery",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-11-30T06:17:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Addressee to collect from depot",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-11-30T09:23:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Delivery attempted - premises closed",
                "coordinates": [],
                "tag": "AttemptFail",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-11-30T16:03:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Held in depot",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-11-30T21:11:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Held in depot",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-12-01T05:20:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Tracked in destination country",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-12-01T06:06:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Prepared for delivery",
                "coordinates": [],
                "tag": "InTransit",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-12-01T08:48:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Addressee advised to collect from Parcelshop",
                "coordinates": [],
                "tag": "AttemptFail",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-12-01T11:41:00",
                "slug": "parcel-force"
            },
            {
                "location": "Delivery Agent - NETHERLANDS",
                "country_name": null,
                "country_iso3": null,
                "state": null,
                "city": null,
                "zip": null,
                "message": "Delivered",
                "coordinates": [],
                "tag": "Delivered",
                "created_at": "2017-12-07T00:48:37+00:00",
                "checkpoint_time": "2017-12-01T19:27:00",
                "slug": "parcel-force"
            }
        ],
        "tracking_account_number": null,
        "tracking_destination_country": null,
        "tracking_key": null,
        "tracking_postal_code": null,
        "tracking_ship_date": null
    },
    "ts": 1512607722
}
php arrays
1个回答
1
投票

您只需要进入$userarr对象内的每个键。请注意,james@emails中的第一个元素,它本身就是一个数组,因此最后一步需要[0]访问第一个索引,或者需要emails键上的循环。

为此,您可以使用方括号表示法:

$userarr["event"]["msg"]["emails"][0] // [email protected]

或箭头符号:

$userarr->event->msg->emails[0] // [email protected]

希望这可以帮助! :)

© www.soinside.com 2019 - 2024. All rights reserved.