Parse Daunting JSON: PHP -
so have huge json chunk of data need parse. has been converted php array json_decode
. below 1 element of data
object in php array. there hundreds of these elements, below one:
'{ "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 } ] }'
below of code far:
$response = json_decode($json_response, true); //$ksortresult = ksort($response['data']); foreach($response $field){ echo $field; }
if can me step through json object arrays, i'd appreciate it. also, i'm trying sort data based on keys before step through it.
the desired output 1 below:
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
so if understand correctly want flatten json structure? if that's case, take @ @ array_walk_recursive:
http://php.net/manual/en/function.array-walk-recursive.php
which this:
$newarray = []; array_walk_recursive( $formerlyjsonarray, function($value, $key) use (&$newarray) { $newarray[$key] = $value; });
or take @ array_reduce:
Comments
Post a Comment