How can I parse JSON file in PHP with structure like this - php

How can I parsing JSON file in PHP with structure like this.
{
"url": "http://lotto.mthai.com",
"result": {
"extractorData": {
"url": "http://lotto.mthai.com",
"resourceId": "85407efc3b77dc0c03350101dbe4d644",
"data": [
{
"group": [
{
"title": [
{
"text": "รางวัลที่ 1"
}
],
"number": [
{
"text": "511825"
}
]
},
{
"title": [
{
"text": "เลขท้าย 2 ตัว"
}
],
"number": [
{
"text": "14"
}
]
},
{
"title": [
{
"text": "รางวัลเลขหน้า 3 ตัว"
}
],
"number": [
{
"text": "111 775"
}
]
},
{
"title": [
{
"text": "รางวัลเลขท้าย 3 ตัว"
}
],
"number": [
{
"text": "880 937"
}
]
},
{
"title": [
{
"text": "รางวัลข้างเคียงรางวัลที่ 1"
}
],
"number": [
{
"text": "511824 511826"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล รางวัลที่ 2"
}
],
"number": [
{
"text": "041316 051696 174632 262383 301461"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 3"
}
],
"number": [
{
"text": "151368 192853 218010 381563 441994 593440 608530 958702 960236 991980"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 4"
}
],
"number": [
{
"text": "005297 143532 183328 308705 457316 561361 692884 813010 891564 984090 027175 151262 203467 309366 464915 580426 725352 835838 895659 986826 034358 160471 207682 426987 477041 626565 727202 840597 927093 990225 055970 175123 221131 428397 542542 674499 761363 883250 936443 994258 100858 175672 285542 434884 558734 692634 795866 888874 967956 996299"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 5"
}
],
"number": [
{
"text": "018823 074535 190371 313865 352942 456747 541518 709349 861423 919589 030122 085212 200070 318968 359324 474167 571715 734617 862093 924308 047376 102785 206781 320576 376900 481372 573928 739412 863966 928572 049905 128721 209050 324031 386258 505554 599073 767614 867382 958601 051129 132159 227324 326875 396408 506582 637094 786639 869522 960148 051242 137750 265952 327702 405928 507898 654254 788560 878668 962359 052418 156993 269239 330428 416971 508844 664963 805061 881503 971836 054447 157814 286374 330762 434541 526775 705692 811851 890915 983191 060979 176311 301301 336489 451863 529673 707115 819033 902456 983528 061270 184563 308537 352516 455643 535855 707396 829248 908825 999541"
}
]
}
]
}
]
},
"pageData": {
"statusCode": 200,
"timestamp": 1464855539981
}
}
}
json file exported from import.io
I want to parsing title and number in group and keep it in array.
I tried many ways but always got error.
Thanks you.

Unless I misunderstood your question you could organize your JSON like below:
"group": [
{
"title": "Some text",
"number": "511825"
},
{
"title": "Some text 2",
"number": "98989"
},

Related

Compare 2 Multidimensional Arrays in PHP and show all the difference in an array

I have 2 Multiple-Arrays and I want to compare them. In the second Array I want then show with colors which value has changed, which key is new and which key is removed.
I have found a lot of different solutions, but no one function for me.
Here are my arrays:
{
"originFormation": {
"stationName": "Bern",
"sections": [{
"name": "B",
"cars": [{
"type": "LOK",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": false,
"nextPassage": false
}]
}, {
"name": "C",
"cars": [{
"type": "CAR",
"number": "1",
"class": "1",
"occupancy": "UNKNOWN",
"attributes": ["AbteilRollstuhl", "AbteilBusiness"],
"closed": false,
"previousPassage": false,
"nextPassage": true
}, {
"type": "CAR",
"number": "1",
"class": "2",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": true,
"nextPassage": true
}]
}],
"legendItems": [{
"id": "1",
"text": "1st class coach",
"refersTo": "type"
}, {
"id": "2",
"text": "2st class coach",
"refersTo": "type"
}, {
"id": "Fahrtrichtung",
"text": "Direction of travel",
"refersTo": "code"
}, {
"id": "FA",
"text": "Family Coach with play area",
"refersTo": "type"
}, {
"id": "AbteilRollstuhl",
"text": "Wheelchair space",
"refersTo": "code"
}, {
"id": "AbteilVeloPl",
"text": "Bicycle loading: Without reservation",
"refersTo": "code"
}, {
"id": "LK",
"text": "Locomotive",
"refersTo": "type"
}, {
"id": "WR",
"text": "Restaurant 1st and 2nd class",
"refersTo": "type"
}, {
"id": "AbteilBusiness",
"text": "Business zone in 1st class: Reservation possible",
"refersTo": "code"
},
{
"id": "HFS_WG_NUM",
"text": "Coach",
"refersTo": "code"
}, {
"id": "KD",
"text": "No passage",
"refersTo": "type"
}
],
"info": {
"title": "Changes to the composition of the train",
"formationChanges": []
}
}
}
Array2:
{
"originFormation": {
"stationName": "Bern",
"sections": [
{
"name": "B",
"cars": [
{
"type": "LOK",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": false,
"nextPassage": false
}
]
},
{
"name": "C",
"cars": [
{
"type": "CAR",
"number": "12",
"class": "1",
"occupancy": "UNKNOWN",
"attributes": [
"AbteilRollstuhl",
"AbteilBusiness"
],
"closed": false,
"previousPassage": false,
"nextPassage": true
},
{
"type": "CAR",
"number": "1",
"class": "2",
"occupancy": "UNKNOWN",
"attributes": [
"AbteilStefan"
],
"closed": false,
"previousPassage": true,
"nextPassage": true
},
{
"type": "CAR",
"number": "2",
"class": "1",
"occupancy": "UNKNOWN",
"attributes": [],
"closed": false,
"previousPassage": true,
"nextPassage": true
}
]
}
],
"legendItems": [
{
"id": "1",
"text": "1st class coach",
"refersTo": "type"
},
{
"id": "2",
"text": "2st class coach",
"refersTo": "type"
},
{
"id": "Fahrtrichtung",
"text": "Direction of travel",
"refersTo": "code"
},
{
"id": "FA",
"text": "Family Coach with play area",
"refersTo": "type"
},
{
"id": "AbteilRollstuhl",
"text": "Wheelchair space",
"refersTo": "code"
},
{
"id": "AbteilVeloPl",
"text": "Bicycle loading: Without reservation",
"refersTo": "code"
},
{
"id": "LK",
"text": "Locomotive",
"refersTo": "type"
},
{
"id": "WR",
"text": "Restaurant 1st and 2nd class",
"refersTo": "type"
},
{
"id": "AbteilBusiness",
"text": "Business zone in 1st class: Reservation possible",
"refersTo": "code"
},
{
"id": "AbteilStefan",
"text": "Ich bin ein neues Abteil",
"refersTo": "code"
},
{
"id": "HFS_WG_NUM",
"text": "Coach",
"refersTo": "code"
},
{
"id": "KD",
"text": "No passage",
"refersTo": "type"
}
],
"info": {
"title": "Changes to the composition of the train",
"formationChanges": [
"All Changed"
]
}
}
}
My fonction to compare:
function array_diff_assoc_recursive($array1, $array2)
{
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!isset($array2[$key]))
{
$difference[$key] = $value;
}
elseif(!is_array($array2[$key]))
{
$difference[$key] = "<b>".$value."</b>";
}
else
{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value)
{
$difference[$key] = "<b>".$value."</b>";
}
}
return !isset($difference) ? 0 : $difference;
}
var_dump(array_diff_assoc_recursive($array2, $array1));
This difference i get:
[originformation][sections][1][cars][0][number] -> newValue 12
[originformation][sections][1][cars][1][attributes] -> newValue AbteilStefan
[originformation][sections][1][cars][2] -> all Attributes are new
[originformation][legendItems][9][id] -> newValue AbteilStefan
[originformation][legendItems][9][text] -> newValue Ich bin ein neues Abteil
[originformation][legendItems][10] -> all Attributes are new
[originformation][legendItems][11] -> all Attributes are new
[originformation][info][formationChanges][0] => newValue All Changed
The difference i expected
[originformation][sections][1][cars][0][number] -> newValue 12
[originformation][sections][1][cars][1] -> all Attributes are new
[originformation][legendItems][9] -> all Attributes are new
[originformation][info][formationChanges][0] => newValue All Changed
Problem
My function doesn't see, that car in Array2 I put in a complete new car and also a complete new legendItem and the rest is still the same.
Does someone know how I can compare the arrays, that he doesn't check the order of the attributes on the same level?

Elastic Search bucket list for a ECommerce Shop / Neast Attributes

can you help me please? I have a ecommerce website with 1000+ products. Each product has a bunch of options like "color", "size", and other specs... but i don't know all the attributes. so i define a document with this mapping:
"mappings" : {
"article" : {
"properties": {
"options": {
"type": "nested",
"include_in_parent":"true",
"properties": {
"id": {"type": "string"},
"name": {"type": "string"},
"values": {"type": "string"}
}
}
}
}
And this is my Query to get the Bucket list:
{
"query": {
"bool": {
"must": [
{
"term": {
"categorie_id": "f52330ce2669dfab884c2d60468b8466"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 1,
"sort": [
{
"sorttype": {
"order": "desc"
}
},
"_score"
],
"aggs": {
"baked_goods": {
"nested": {
"path": "options"
},
"aggs": {
"name": {
"terms": {
"field": "id"
},
"aggs": {
"name": {
"terms": {
"field": "values"
}
}
}
}
}
}
}
}
I get Documents, but the Result of the Buckets is Empty...
"aggregations": {
"baked_goods": {
"doc_count": 3331,
"name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [ ]
}
}
}
i want something like:
"color" => "red" (4)
"color" => "blue" (2)
"size" => "X" (11)
..
Can you please help me??
i found a solution.
Mapping:
"options": {
"type": "nested",
"include_in_parent": true,
"properties": {
"name": { "type": "text" , "analyzer": "whitespace", "fielddata": true},
"values": { "type": "text" , "analyzer": "whitespace", "fielddata": true}
}
}
Query:
"aggs": {
"facets": {
"nested": {
"path": "options"
},
"aggs": {
"name": {
"terms": {
"field": "options.name"
},
"aggs": {
"name": {
"terms": {
"field": "options.values"
}
}
}
}
}
} }

SurveyMonkey: Filter get_survey_details response

I'm trying to retrieve the responses for a particular survey from Survey Monkey using the get_responses endpoint which only retrieves the "col" and "row" ids.
{
"status": 0,
"data": [
{
"respondent_id": "4165059283",
"questions": [
{
"answers": [
{
"text": "04/11/1990",
"row": "9333771686"
}
],
"question_id": "842633241"
},
{
"answers": [
{
"row": "9333771693"
},
{
"text": "aa",
"row": "9333771691"
}
],
"question_id": "842633242"
},
{
"answers": [
{
"col": "9333771714",
"row": "9333771713"
},
{
"col": "9333771717",
"row": "9333771712"
},
{
"col": "9333771714",
"row": "9333771711"
},
{
"col": "9333771717",
"row": "9333771710"
},
{
"col": "9333771717",
"row": "9333771709"
},
{
"col": "9333771716",
"row": "9333771708"
},
{
"col": "9333771714",
"row": "9333771707"
},
{
"col": "9333771716",
"row": "9333771706"
},
{
"col": "9333771716",
"row": "9333771705"
},
{
"col": "9333771715",
"row": "9333771704"
},
{
"col": "9333771715",
"row": "9333771703"
},
{
"col": "9333771714",
"row": "9333771702"
},
{
"text": "aa",
"row": "0"
}
],
"question_id": "842633243"
},
{
"answers": [
{
"col": "9333771729",
"row": "9333771726"
},
{
"col": "9333771727",
"row": "9333771725"
},
{
"col": "9333771727",
"row": "9333771724"
},
{
"text": "aaa",
"row": "0"
}
],
"question_id": "842633244"
},
]
}
]
}
I can see that these id's correlate to a question and answer from the get_survey_details endpoint..
But how on earth do you filter the get_survey_details response to only show the question/answer that the respondent has given rather than retrieve everything? I've tried adding to the $params in the class as shown but the request still gives everything back... so clearly this isn't possible or I'm going about it in completely the wrong way:
public function getSurveyDetails($surveyId){
//original $params
//$params = array('survey_id'=>$surveyId);
$params = array(
'survey_id'=>$surveyId,
'pages'=>array(
'questions' => array (
'answers' => array (
'answer_id' => '9333771686',
)
),
)
);
return $this->run('surveys/get_survey_details', $params);
}

How do you access json nested elements with php

For 24hrs I have been really struggling to find how to access the email and address fields. I think I've tried every combination there is
my last attempt
$obj = json_decode($json);
$emails = $obj->emails->address;
JSON OUTPUT
{
"#http_status_code": 200,
"#visible_sources": 1,
"#available_sources": 1,
"#search_id": "1507310336407868146373527620323253083",
"query": {
"emails": [
{
"address": "paulsparktest#googlemail.com",
"address_md5": "d92590f691eab834586686c563567382"
}
]
},
"possible_persons": [
{
"#search_pointer": "3984f5b332b6efb3331bb137e1e97829ddff0971d9de9347cbd7fb8f82dc68de093a525a66584694339bfe580baff2aacb77954e0f154a1d0bd0a36588094972a72c1c4a63197a9736f6c425afdf66e5d8e52d35073d6499036efe9a234dd1d886f71bf54b9911a19725f118b6cd7bca521c246fe3b890a957596f8236c3c4ac5ba241198c3bdfa2f44a4e361393f1bf407130ffb5b9e2f6b1ccffca87befd0b147e51a12a54773ca31fc1a364b8cde876ca5f42b5d6f0c319f18300cab29fc1",
"names": [
{
"first": "Paul",
"last": "Johnson",
"display": "Paul Johnson"
}
],
"usernames": [
{
"content": "pauljohnson111"
}
],
"gender": {
"#inferred": true,
"content": "male"
},
"addresses": [
{
"country": "GB",
"state": "ENG",
"city": "London",
"display": "London, England"
}
],
"user_ids": [
{
"content": "374967130#twitter"
}
],
"images": [
{
"url": "http://pbs.twimg.com/profile_images/1546592694/Roccc.jpg",
"thumbnail_token": "AE2861B242686E7DDBDF0D814A3486E1D19BE9609F41B4AA71B6D0FEB03454A84C36C69AC788EF676B93C5274D29CD76361050E1F057871D&dsid=39844"
}
]
}
]
}
This will get the address for you
$obj->query->emails[0]->address;
if you want to iterate and get other addresses
foreach ( $obj->query->emails as $email ) {
echo $email->address;
}
You can get address & name via follow code.
// Getting email
echo $data->query->emails[0]->address;
// Getting name
echo $data->possible_persons[0]->names[0]->display;
Full code
$data = '{
"#http_status_code": 200,
"#visible_sources": 1,
"#available_sources": 1,
"#search_id": "1507310336407868146373527620323253083",
"query": {
"emails": [
{
"address": "paulsparktest#googlemail.com",
"address_md5": "d92590f691eab834586686c563567382"
}
]
},
"possible_persons": [
{
"#search_pointer": "3984f5b332b6efb3331bb137e1e97829ddff0971d9de9347cbd7fb8f82dc68de093a525a66584694339bfe580baff2aacb77954e0f154a1d0bd0a36588094972a72c1c4a63197a9736f6c425afdf66e5d8e52d35073d6499036efe9a234dd1d886f71bf54b9911a19725f118b6cd7bca521c246fe3b890a957596f8236c3c4ac5ba241198c3bdfa2f44a4e361393f1bf407130ffb5b9e2f6b1ccffca87befd0b147e51a12a54773ca31fc1a364b8cde876ca5f42b5d6f0c319f18300cab29fc1",
"names": [
{
"first": "Paul",
"last": "Johnson",
"display": "Paul Johnson"
}
],
"usernames": [
{
"content": "pauljohnson111"
}
],
"gender": {
"#inferred": true,
"content": "male"
},
"addresses": [
{
"country": "GB",
"state": "ENG",
"city": "London",
"display": "London, England"
}
],
"user_ids": [
{
"content": "374967130#twitter"
}
],
"images": [
{
"url": "http://pbs.twimg.com/profile_images/1546592694/Roccc.jpg",
"thumbnail_token": "AE2861B242686E7DDBDF0D814A3486E1D19BE9609F41B4AA71B6D0FEB03454A84C36C69AC788EF676B93C5274D29CD76361050E1F057871D&dsid=39844"
}
]
}
]
}';
$data = json_decode($data);
// Getting email
echo $data->query->emails[0]->address;
// Getting name
echo $data->possible_persons[0]->names[0]->display;

Parse a dynamic JSON object into PHP

Below is the JSON object I am trying to parse. It its basically the answers to a dynamic form. I know I can convert it to an array and then work with it. The issue I am having is the JSON object will be different every time.
{
"sections": [
{
"elements": [
{
"element": "greeting",
"components": {
"paragraph": {
"content": "Thank you for calling company name. This is a test call."
}
}
},
{
"element": "custom-fullname",
"components": {
"label": {
"content": ""
},
"first_name": {
"name": "1[custom-fullname][first_name]",
"label": "First Name",
"type": "text",
"value": "John",
"required": ""
},
"last_name": {
"name": "1[custom-fullname][last_name]",
"label": "Last Name",
"type": "text",
"value": "smith",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "custom-phone",
"components": {
"label": {
"content": "Phone Number"
},
"phone_ext": {
"name": "2[custom-phone][phone_ext]",
"type": "text",
"value": ""
},
"input_format": {
"name": "2[custom-phone][phone_format]",
"type": "hidden",
"value": "standard"
},
"help_text": {
"content": ""
},
"phone": {
"value": "7864564444",
"required": "false",
"name": "2[custom-phone][phone]"
}
}
},
{
"element": "custom-message",
"components": {
"label": {
"content": "Message"
},
"message": {
"name": "3[custom-message][message]",
"type": "text",
"value": "test time 22222",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "notes",
"components": {
"paragraph": {
"content": "This is a test call. Please mark this as a test."
}
}
}
],
"faqs": [
{
"question": "Should i have a FAQ?",
"answer": "Yes, this will help the agent to have a fluent conversation with the caller."
}
]
},
{
"elements": [
{
"element": "greeting",
"components": {
"paragraph": {
"content": "Thank you for calling company name. This is a test call."
}
}
},
{
"element": "custom-fullname",
"components": {
"label": {
"content": ""
},
"first_name": {
"name": "1[custom-fullname][first_name]",
"label": "First Name",
"type": "text",
"value": "robert",
"required": ""
},
"last_name": {
"name": "1[custom-fullname][last_name]",
"label": "Last Name",
"type": "text",
"value": "gonzalez",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "custom-phone",
"components": {
"label": {
"content": "Phone Number"
},
"phone_ext": {
"name": "2[custom-phone][phone_ext]",
"type": "text",
"value": ""
},
"input_format": {
"name": "2[custom-phone][phone_format]",
"type": "hidden",
"value": "standard"
},
"help_text": {
"content": ""
},
"phone": {
"value": "7864564444",
"required": "false",
"name": "2[custom-phone][phone]"
}
}
},
{
"element": "custom-message",
"components": {
"label": {
"content": "Message"
},
"message": {
"name": "3[custom-message][message]",
"type": "text",
"value": "Test",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "notes",
"components": {
"paragraph": {
"content": "This is a test call. Please mark this as a test."
}
}
}
],
"faqs": [
{
"question": "Should i have a FAQ?",
"answer": "Yes, this will help the agent to have a fluent conversation with the caller."
}
]
},
{
"elements": [
{
"element": "greeting",
"components": {
"paragraph": {
"content": "Thank you for calling company name. This is a test call."
}
}
},
{
"element": "custom-fullname",
"components": {
"label": {
"content": ""
},
"first_name": {
"name": "1[custom-fullname][first_name]",
"label": "First Name",
"type": "text",
"value": "robert",
"required": ""
},
"last_name": {
"name": "1[custom-fullname][last_name]",
"label": "Last Name",
"type": "text",
"value": "gonzalez",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "custom-phone",
"components": {
"label": {
"content": "Phone Number"
},
"phone_ext": {
"name": "2[custom-phone][phone_ext]",
"type": "text",
"value": ""
},
"input_format": {
"name": "2[custom-phone][phone_format]",
"type": "hidden",
"value": "standard"
},
"help_text": {
"content": ""
},
"phone": {
"value": "7861238975",
"required": "false",
"name": "2[custom-phone][phone]"
}
}
},
{
"element": "custom-message",
"components": {
"label": {
"content": "Message"
},
"message": {
"name": "3[custom-message][message]",
"type": "text",
"value": "test time 22222",
"required": ""
},
"help_text": {
"content": ""
}
}
},
{
"element": "radio",
"components": {
"label": {
"content": "Whats type of office are you looking for?"
},
"radiolist": [
{
"input": {
"value": "value_0",
"id": "single_choice_answer_0"
},
"label": {
"content": "Dedicated Space"
}
},
{
"input": {
"value": "value_1",
"id": "single_choice_answer_1"
},
"label": {
"content": "Parttime Space"
}
}
],
"single_choice_answer": {
"value": "value_0",
"name": "4[radio][single_choice_answer]",
"required": ""
},
"help_text": {
"content": ""
}
}
}
],
"faqs": [
{
"question": "Should i have a FAQ?",
"answer": "Yes, this will help the agent to have a fluent conversation with the caller."
}
]
}
]
}
So, for example a part of the JSON object looks like the following.
"components": {
"label": {
"content": "Whats type of office are you looking for?"
},
"radiolist": [
{
"input": {
"value": "value_0",
"id": "single_choice_answer_0"
},
"label": {
"content": "Dedicated Space"
}
},
{
"input": {
"value": "value_1",
"id": "single_choice_answer_1"
},
"label": {
"content": "Parttime Space"
}
}
],
"single_choice_answer": {
"value": "value_0",
"name": "4[radio][single_choice_answer]",
"required": ""
},
I would like to take that and do something like the following:
{
"components": {
"label": {
"content": "Whats type of office are you looking for?"
},
"answer": {
"value": "value_0",
"name": "Dedicated Space",
"required": ""
}
}
Really I just need the form questions and answer formatted a little nicer in a JSON object. However, I'm not sure how to code this in PHP.

Categories