【问题标题】:Decoding a multidimensional Json array and seperate it into 3 different arrays解码多维 Json 数组并将其分成 3 个不同的数组
【发布时间】:2017-10-02 15:33:07
【问题描述】:

我正在从 web 服务接收下面的 json 有效负载,我试图在解码 whlist 后使用循环将其分成 3 个不同的数组,保持相同的数组键:

{
"id": 5705376,
"title": "Satellite Installation",
"created_at": "2017-09-07T14:02:19.000Z",
"updated_at": "2017-09-07T14:02:19.000Z",
"customer_id": 3126803,
"way_points": [{
    "id": 7405587,
    "lat": -26.0578251,
    "lng": 28.02189520000002,
    "task_id": 5705376,
    "done": false,
    "allow_editing_inventory": true,
    "customer_contact_ids": [3126803],
    "customer": {
        "original_phone_number": null,
        "last_open_at": null,
        "last_order_at": null,
        "uploaded_profile_image": {
            "url": "/images/avatar.png"
        },
        "original_lat": null,
        "original_lng": null,
        "district": null,
        "house_number": null,
        "street": null
    },
    "full_address": "8 Anslow Ln, Bryanston, Sandton, 2191, South Africa"
}],
"customer": {
    "id": 3126803,
    "name": "Lance",
    "address": "8 Anslow Ln, Bryanston, Sandton, 2191, South Africa",
    "lat": -26.0578251,
    "lng": 28.0218952,
    "created_at": "2017-08-29T10:00:32.360Z",
    "updated_at": "2017-09-07T14:02:19.860Z",
    "phone": null,
    "merchant_id": 11221,
    "allow_login": false,
    "stripe_id": null,
    "original_phone_number": null,
    "last_open_at": null,
    "last_order_at": null,
    "uploaded_profile_image": {
        "url": "/images/avatar.png"
    },
    "original_lat": null,
    "original_lng": null,
    "house_number": null,
    "street": null
},
"late": false,
"external_id": "5705376",
"uuid": "28095e33-b30c-4e35-98d2-aae6ad428f66",
"dispatcher_id": null,
"team_ids": [12422],
"ready_to_execute": true,
"tip_driver_enabled": false,
"automatically_assigned": false,
"task_inventories": null,
"task_notes": null
}

对于“way_points”键中的值,我想在解码后提出一个称为航点的数组,同时忽略其中的客户数组。我也想在这个过程中把“id”键名改成“waypoint_id”。

对于“customers”键中的值(在主数组中),我想在解码后提出一个名为“waypoint”的数组,同时分配“url”的值 键到“上传的个人资料图像”键。我也想在这个过程中把“id”键名改成“customer_id”。

上述数组之外的任何元素(因此主数组中未分配给数组的元素),我希望它们形成一个称为“订单”的数组。

我该怎么做。我是 LOOPS 的新手

【问题讨论】:

  • 为什么需要不同的变量?只需使用 json_decode() 解码 json 即可获得所需的一切。
  • 您分享的 JSON 格式无效,您确定那是正确的吗?
  • 我对无效的 json 感到抱歉,我编辑了我的答案

标签: php arrays json multidimensional-array


【解决方案1】:

首先,您需要将 json 编码字符串解码为可用格式,array 或对象。您可以找到json_decode here 的所有可能选项。

$inputArray = json_decode($inputString, true);

现在循环。为简单起见,您可以将所需的三个数组初始化为空数组。然后,在循环中,只需检查键值并选择要将数据附加到哪个数组。

$waypoints = array();
$customers = array();
$orders = array();

$count = 0;
foreach($inputArray as $key => $element) {
    /* using if statements */
    if($key === 'way_points') {
        $waypoints[] = $element;
    }
    elseif($key === 'customer') {
        $customers[] = $element;
    }
    else {
        $orders[$count][$key] = $element;
    }

    /* using a switch/case */
    switch($key) {
        case 'way_points':
            $waypoints[] = $element;
            break;
        case 'customer':
            $customers[] = $element;
            break;
        default:
            $orders[$count][$key] = $element;
            break;
    }

    $count++;
}

此时您应该拥有所需的所有数据,但我们仍然没有更改密钥。你可以保持原样,毕竟id 中的$waypoints 键代表waypoint_id 是不言自明的。但是,如果您确实需要更改密钥,有几种方法可以做到这一点。您可以遍历新形成的$waypoints 数组并在这个新循环中修改键。

$waypoints = change_keys($waypoints);

function change_keys($arr) {
    return array_map(function($waypoint) {
        return array(
            'waypoint_id' => $waypoint['id'],
            'lat' => $element['lat'],
            /* remaining fields */
        );
    }, $arr);
}

或者你可以减少步骤并在初始foreach循环中进行

$waypoints = array();
$customers = array();
$orders = array();

$count = 0;
foreach($inputArray as $key => $element) {
    if($key === 'way_points') {
        $waypoints[] = array_map(function($element) {
            return array(
                'waypoint_id' => $element['id'],
                'lat' => $element['lat'],
                /* remaining fields */
            );
        }, $arr);
    }

    /* ... */
}

你可以在array_maphere找到更多信息。


顺便说一句,正如 @jeroen 在 cmets 中提到的,您可以简单地使用 json_decode,然后您将得到一个可用的关联数组。例如,要遍历航点,您只需编写 foreach($myArray[waypoints] as $key => $waypoint)

【讨论】:

    猜你喜欢
    • 2016-07-07
    • 1970-01-01
    • 2011-04-11
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    相关资源
    最近更新 更多