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;
Related
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?
I need to add a new node to a deep nested json file. I cant figure out how to get the array index to which the node is to be inserted.
This is for accessing and managing collaterals in JSON file.I have tried to use array_split() function but the third parameter only adds node with one value.
Sample JSON File:
{
"IsNew":"0",
"Title":"Industry",
"View":"grid",
"File":"",
"Items":[
{
"IsNew":"0",
"Title":"Industrial Products",
"Image":"Industrial-Products.png",
"View":"list",
"Items":[
{
"IsNew":"0",
"Title":"Offerings",
"View":"detaillist",
"Items":[
]
},
{
"IsNew":"0",
"Title":"Collateral",
"View":"multidetaillist",
"Items":[
{
"IsNew":"0",
"Title":"Flyers and Brochures",
"Subtitle":"",
"Items":[
{
"IsNew":"0",
"Title":"Building Solutions",
"View":"pdf",
"File":"Building_Solutions.pdf",
"Type":"1"
},
{
"IsNew":"0",
"Title":"Industrial Machinery",
"View":"pdf",
"File":"Industrial_Machinery.pdf",
"Type":"1"
}
]
}
]
}
]
}
]
}
Node To Add in Flyers and Brochures items:
{
"IsNew": "0",
"Title": "abc",
"Image": "abc",
"View": "pdf",
"File": "abc",
"Type": "1"
}
Note: Your original sample JSON file is not valid. It is missing some closing parenthesis which I've taken the liberty of fixing in the code below.
The php code for appending your item is:
$jsonStr = '{
"IsNew": "0",
"Title": "Industry",
"View": "grid",
"File": "",
"Items": [
{
"IsNew": "0",
"Title": "Industrial Products",
"Image": "Industrial-Products.png",
"View": "list",
"Items": [
{
"IsNew": "0",
"Title": "Offerings",
"View": "detaillist",
"Items": []
}
,
{
"IsNew": "0",
"Title": "Collateral",
"View": "multidetaillist",
"Items": [
{
"IsNew": "0",
"Title": "Flyers and Brochures",
"Subtitle": "",
"Items": [
{
"IsNew": "0",
"Title": "Building Solutions",
"View": "pdf",
"File": "Building_Solutions.pdf",
"Type": "1"
},
{
"IsNew": "0",
"Title": "Industrial Machinery",
"View": "pdf",
"File": "Industrial_Machinery.pdf",
"Type": "1"
}
]
}]}]}]}';
$insertStr = '{
"IsNew": "0",
"Title": "abc",
"Image": "abc",
"View": "pdf",
"File": "abc",
"Type": "1"
}';
// turns json strings into objects
$obj = json_decode($jsonStr);
$insertObj = json_decode($insertStr);
// target the node is $obj->Items[0]->Items[1]->Items[0]->Items
// so we just append the object to the node
$obj->Items[0]->Items[1]->Items[0]->Items[] = $insertObj;
$newJson = json_encode($obj);
echo($newJson);
This will result in:
{
"IsNew":"0",
"Title":"Industry",
"View":"grid",
"File":"",
"Items":[
{
"IsNew":"0",
"Title":"Industrial Products",
"Image":"Industrial-Products.png",
"View":"list",
"Items":[
{
"IsNew":"0",
"Title":"Offerings",
"View":"detaillist",
"Items":[
]
},
{
"IsNew":"0",
"Title":"Collateral",
"View":"multidetaillist",
"Items":[
{
"IsNew":"0",
"Title":"Flyers and Brochures",
"Subtitle":"",
"Items":[
{
"IsNew":"0",
"Title":"Building Solutions",
"View":"pdf",
"File":"Building_Solutions.pdf",
"Type":"1"
},
{
"IsNew":"0",
"Title":"Industrial Machinery",
"View":"pdf",
"File":"Industrial_Machinery.pdf",
"Type":"1"
},
{
"IsNew":"0",
"Title":"abc",
"Image":"abc",
"View":"pdf",
"File":"abc",
"Type":"1"
}
]
}
]
}
]
}
]
}
try this json...
{
"IsNew": "0",
"Title": "Industry",
"View": "grid",
"File": "",
"Items": [
{
"IsNew": "0",
"Title": "Industrial Products",
"Image": "Industrial-Products.png",
"View": "list",
"Items": [
{
"IsNew": "0",
"Title": "Offerings",
"View": "detaillist",
"Items": []
},
{
"IsNew": "0",
"Title": "Collateral",
"View": "multidetaillist",
"Items": [
{
"IsNew": "0",
"Title": "Flyers and Brochures",
"Subtitle": "",
"Items": [
{
"IsNew": "0",
"Title": "Building Solutions",
"View": "pdf",
"File": "Building_Solutions.pdf",
"Type": "1"
},
{
"IsNew": "0",
"Title": "Industrial Machinery",
"View": "pdf",
"File": "Industrial_Machinery.pdf",
"Type": "1"
},
{
"IsNew": "0",
"Title": "abc",
"Image": "abc",
"View": "pdf",
"File": "abc",
"Type": "1"
}
]
}
]
}
]
}
]
}
I want JSON object as follows in that personal, address and itm have sequence of json object.
{
"id": "1",
"state": "12",
"personal": [
{
"name": "abc",
"contact":"1111111"
"address": [
{
"line1": "abc",
"city": "abc",
"itm": [
{
"num": 1,
"itm_detatils": {
"itemname": "bag",
"rate": 1000,
"discount": 0,
}
}
],
"status": "Y"
}
]
}
]
}
But I am getting result as follows in that I want json array at address and itm_details.
{
"id": "1",
"state": "12",
"personal": [
{
"name": "abc",
"contact": "1111111",
"address": {
"line1": "abc",
"city": "abc",
"itm": {
"inum": "1",
"itm_detatils": {
"itemname": "bag",
"rate": 1000,
"discount": 0
}
},
"status": "Y"
}
}
]
}
My PHP Code is as follow:
In that I am creating simple array and after that array inside array but during encoding to json it's not showing sequence of json object.
$a=array();
$a["id"]="1";
$a["state"]="12";
$a["personal"]=array();
$a["personal"][]=array(
"name"=>"abc",
"contact"=>"1111111",
"address"=>array(
"line1"=>"abc",
"city"=>"abc",
"itm"=>array(
"inum"=>"1",
"itm_detatils"=>array(
"itemname"=>"bag",
"rate"=>1000,
"discount"=>0,
),
),
"status"=>"Y",
),
);
echo json_encode($a);
Thanks in advance.
Add one more array
//...
"address" => array(
array(
"line1"=>"abc",
"city"=>"abc",
// ...
),
)
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"
},
I am working with drupal 8. I am trying to get the JSON of all nodes of the content type. I got a json as given bellow. But Now I want to change the Following JSON to
[
{
"nid": [
{
"value": "17"
}
],
"uuid": [
{
"value": "3614e0c8-88d4-4e8d-a732-5089698556d5"
}
],
"vid": [
{
"value": "17"
}
],
"type": [
{
"target_id": "resume_creator"
}
],
"langcode": [
{
"value": "en"
}
],
"title": [
{
"value": "uyi"
}
],
"uid": [
{
"target_id": "1"
}
],
"status": [
{
"value": "1"
}
],
"created": [
{
"value": "1452060690"
}
],
"changed": [
{
"value": "1452060709"
}
],
"promote": [
{
"value": "1"
}
],
"sticky": [
{
"value": "0"
}
],
"revision_timestamp": [
{
"value": "1452060709"
}
],
"revision_uid": [
{
"target_id": "1"
}
],
"revision_log": [],
"revision_translation_affected": [
{
"value": "1"
}
],
"default_langcode": [
{
"value": "1"
}
],
"path": [],
"field_communication_address": [
{
"value": "rtyrtytr\r\nuu;\r\nsdgfdh"
}
],
"field_education": [
{
"value": "ytutyuii"
}
],
"field_emails": [
{
"value": "gtf#fgfg.com"
}
],
"field_experiece": [
{
"value": "fghtutyu"
}
],
"field_name": [
{
"value": "ytt"
}
]
}
]
to a format of
[
{
"nid":"17",
"uuid":"3614e0c8-88d4-4e8d-a732-5089698556d5",
"vid": "17",
"type":"resume_creator",
"langcode":"en",
"title":"uyi",
"uid":"1",
"status":"1",
"created":"1452060690",
"changed":"1452060709",
"promote":"1",
"sticky":"0",
"revision_timestamp":"1452060709",
"revision_uid":"1",
"revision_log": [],
"path":[],
"field_communication_address":"rtyrtytr\r\nuu;\r\nsdgfdh",
"field_education":"ytutyuii",
"field_emails":"gtf#fgfg.com",
"field_experiece":"fghtutyu",
"field_name":"ytt"
}
]
using php. Then only I can manage a form angular js. Thanks in advance
Try this
$json = '{
"nid": [
{
"value": "17"
}
],
"uuid": [
{
"value": "3614e0c8-88d4-4e8d-a732-5089698556d5"
}
],
"vid": [
{
"value": "17"
}
],
"type": [
{
"target_id": "resume_creator"
}
],
"langcode": [
{
"value": "en"
}
],
"title": [
{
"value": "uyi"
}
],
"uid": [
{
"target_id": "1"
}
],
"status": [
{
"value": "1"
}
],
"created": [
{
"value": "1452060690"
}
],
"changed": [
{
"value": "1452060709"
}
],
"promote": [
{
"value": "1"
}
],
"sticky": [
{
"value": "0"
}
],
"revision_timestamp": [
{
"value": "1452060709"
}
],
"revision_uid": [
{
"target_id": "1"
}
],
"revision_log": [],
"revision_translation_affected": [
{
"value": "1"
}
],
"default_langcode": [
{
"value": "1"
}
],
"path": [],
"field_communication_address": [
{
"value": "rtyrtytr\r\nuu;\r\nsdgfdh"
}
],
"field_education": [
{
"value": "ytutyuii"
}
],
"field_emails": [
{
"value": "gtf#fgfg.com"
}
],
"field_experiece": [
{
"value": "fghtutyu"
}
],
"field_name": [
{
"value": "ytt"
}
]
}';
$json = json_decode($json,true);
foreach ($json as $key => $value){
if(isset($json[$key][0]['value'])){
$json[$key] = $json[$key][0]['value'];
}
if(isset($json[$key][0]['target_id'])){
$json[$key] = $json[$key][0]['target_id'];
}
// $json[$key] = $json[$key][0]['value'];
}
$json = json_encode($json);
print_r($json);
It is simple.
<?php
$arr = array('nid' => 17, 'uuid' => '3614e0c8-88d4-4e8d-a732-5089698556d5', ...);
echo json_encode($arr);
?>
If you have some misunderstanding, ask me.