解析令人生畏的JSON:PHP

时间:2015-07-21 20:26:04

标签: php json

所以我需要解析一个巨大的JSON数据块。它已被转换为json_decode的PHP数组。下面是PHP数组中data对象的一个​​元素。这些元素有数百个,下面只有一个:

'{
    "data": [
        {
            "id": 3215,
            "user_id": {
                "id": 99106,
                "name": "Rusty shackleford",
                "email": "Rusty.shackleford@company.com",
                "has_pic": true,
                "pic_hash": "someHash",
                "active_flag": true,
                "value": 99106
            },
            "person_id": {
                "name": "rusty shackleford",
                "email": [
                    {
                        "label": "Work",
                        "value": "rusty shackleford@email.com",
                        "primary": true
                    }
                ],
                "phone": [
                    {
                        "label": "Fax",
                        "value": "666-666-6666",
                        "primary": false
                    },
                    {
                        "label": "main",
                        "value": "666-666-6666",
                        "primary": false
                    },
                    {
                        "label": "main",
                        "value": "666-666-6666",
                        "primary": true
                    }
                ],
                "value": 666
            },
            "org_id": {
                "name": "shackleford, Inc.",
                "people_count": 23,
                "cc_email": "rusty@6theShack.com",
                "value": 1013
            },
            "stage_id": 8,
            "title": "rusty\'s Projects",
            "value": 0,
            "currency": "USD",
            "add_time": "2013-01-15 00:00:00",
            "update_time": "2015-07-07 14:28:15",
            "stage_change_time": "2013-10-30 14:43:09",
            "active": true,
            "deleted": false,
            "status": "open",
            "next_activity_date": "2015-07-07",
            "next_activity_time": null,
            "next_activity_id": 3771,
            "last_activity_id": 252,
            "last_activity_date": "2013-11-16",
            "lost_reason": null,
            "visible_to": "3",
            "close_time": null,
            "pipeline_id": 1,
            "won_time": null,
            "lost_time": null,
            "products_count": null,
            "files_count": null,
            "notes_count": 21,
            "followers_count": 1,
            "email_messages_count": null,
            "activities_count": 2,
            "done_activities_count": 1,
            "undone_activities_count": 1,
            "reference_activities_count": 0,
            "participants_count": 1,
            "b98336b40c8420dc2f1401d6451b1b47198eee6d": "",
            "17a14a9da9815451ff5ffc669d407e8b0376b06b": 4616,
            "3eedd4fd08f44a72d911dc4934a6916f3b31911b": "",
            "52ede287f6c55eb6b12821ca24f74098779abdce": "",
            "13916ba291ab595f27aefbff8b6c43a3fb467b72": "shackleford LLP",
            "6330684838740625ea6a7d260f102a1961b2fae1": "shackleford, Inc.",
            "ded823307920bf70cea49c45684148fd295e179a": "",
            "ed35f69413af7156058d1081321e7bb227577eef_lat": null,
            "ed35f69413af7156058d1081321e7bb227577eef_long": null,
            "ed35f69413af7156058d1081321e7bb227577eef_subpremise": null,
            "ed35f69413af7156058d1081321e7bb227577eef_street_number": null,
            "ed35f69413af7156058d1081321e7bb227577eef_route": null,
            "ed35f69413af7156058d1081321e7bb227577eef_sublocality": null,
            "ed35f69413af7156058d1081321e7bb227577eef_locality": null,
            "ed35f69413af7156058d1081321e7bb227577eef_admin_area_level_1": null,
            "ed35f69413af7156058d1081321e7bb227577eef_admin_area_level_2": null,
            "ed35f69413af7156058d1081321e7bb227577eef_country": null,
            "ed35f69413af7156058d1081321e7bb227577eef_postal_code": null,
            "ed35f69413af7156058d1081321e7bb227577eef_formatted_address": null,
            "09358ea07e1a1007d24bc068c723bf1f79e8359d": null,
            "expected_close_date": null,
            "7cce64c3abb5f28a260bc9d6719a43367bae5dfe": null,
            "stage_order_nr": 10,
            "person_name": "shackleford",
            "org_name": "shackleford, Inc.",
            "next_activity_subject": "pocket Sand!",
            "next_activity_type": "task",
            "next_activity_duration": "00:00:00",
            "next_activity_note": "",
            "formatted_value": "$0",
            "weighted_value": 0,
            "formatted_weighted_value": "$0",
            "rotten_time": null,
            "owner_name": "Rusty shackleford",
            "cc_email": "rusty+stuff3215@shackleford.com",
            "org_hidden": false,
            "person_hidden": false
        }
    ]
}'

以下是我目前的一些代码:

$response = json_decode($json_response, true);
//$ksortResult = ksort($response['data']);
foreach($response as $field){
    echo $field;
}

如果有人可以帮助我通过数组逐步浏览json对象,我将非常感激。此外,我正在尝试基于键对数据进行排序。

所需的输出将如下所示:

id|user_id|person_id|org_id|stage_id|title|value|currency|add_time|update_time|stage_change_time|active|deleted|status|next_activity_date|next_activity_time|next_activity_id|last_activity_id|last_activity_date|lost_reason|visible_to|close_time|pipeline_id|won_time|lost_time|products_count|files_count|notes_count|followers_count|email_messages_count|activities_count|done_activities_count|undone_activities_count|reference_activities_count|participants_count|b98336b40c8420dc2f1401d6451b1b47198eee6d|_17a14a9da9815451ff5ffc669d407e8b0376b06b|_3eedd4fd08f44a72d911dc4934a6916f3b31911b|_52ede287f6c55eb6b12821ca24f74098779abdce|_13916ba291ab595f27aefbff8b6c43a3fb467b72|_6330684838740625ea6a7d260f102a1961b2fae1|ded823307920bf70cea49c45684148fd295e179a ed35f69413af7156058d1081321e7bb227577eef_lat    ed35f69413af7156058d1081321e7bb227577eef_long|ed35f69413af7156058d1081321e7bb227577eef_subpremise|ed35f69413af7156058d1081321e7bb227577eef_street_number|ed35f69413af7156058d1081321e7bb227577eef_route|ed35f69413af7156058d1081321e7bb227577eef_sublocality|ed35f69413af7156058d1081321e7bb227577eef_locality|ed35f69413af7156058d1081321e7bb227577eef_admin_area_level_1|ed35f69413af7156058d1081321e7bb227577eef_admin_area_level_2|ed35f69413af7156058d1081321e7bb227577eef_country|ed35f69413af7156058d1081321e7bb227577eef_postal_code|ed35f69413af7156058d1081321e7bb227577eef_formatted_address|_09358ea07e1a1007d24bc068c723bf1f79e8359d|expected_close_date|_7cce64c3abb5f28a260bc9d6719a43367bae5dfe|stage_order_nr|person_name  org_name|next_activity_subject|next_activity_type|next_activity_duration|next_activity_note|formatted_value|weighted_value|formatted_weighted_value|rotten_time owner_name|cc_email|org_hidden|person_hidden|user_name|user_email|person_phone_1    person_phone_2|person_phone_3|org_people_count
3215|99106|666|1013|8|rusty's Projects|0|USD|1/15/2013 0:00|7/7/2015 14:28|10/30/2013 14:43|TRUE|FALSE|open|7/7/2015|null|3771|252|11/16/2013|null|3|null|1|null|null|null|null|21|1|null|2|1|1|0|1||4616|||shackleford LLP|shackleford,Inc.||null|null|null|null|null|null|null|null|null|null|null|null|null|null|null|10|shackleford|shackleford, Inc.|pocket Sand!|task|0:00:00||$0|0|$0|0:00:00|Rusty shackleford|rusty+stuff3215@shackleford.com|FALSE|FALSE|Rusty shackleford|Rusty.shackleford@company.com|666-666-6666|666-666-6666|666-666-6666|23

1 个答案:

答案 0 :(得分:1)

所以,如果我理解正确你想要展平json结构?如果是这种情况,请看一下array_walk_recursive:

http://php.net/manual/en/function.array-walk-recursive.php

看起来像这样:

$newArray = [];

array_walk_recursive( $formerlyJsonArray, 
    function($value, $key) use (&$newArray) {
        $newArray[$key] = $value; 
});

或者看看array_reduce:

http://php.net/manual/en/function.array-reduce.php