Related
I have an object like this, and I'm working with "todos" array, only want update active status with specific indexs are given:
{
"_id": "61e7e78372d3221d2c5fb242",
"from_date": "2022/01/01 00:00:00",
"to_date": "2022/01/01 00:00:00",
"todos": [
{
"name": "sub1",
"desc": "desc1",
"active": true,
"owner": "61e6125db0f102060951aa53"
}, // index = 0
{
"name": "sub2",
"desc": "desc2",
"active": true,
"owner": "61e6125db0f102060951aa53"
}, // index = 1
...
]
}
The first I try update with: ['$set' => ['todos.0.active' => false]]
{
"0": {
"active": false
},
"name": "sub1",
"desc": "desc1",
"active": true,
"owner": "61e6125db0f102060951aa53"
},
active can be update to false, key 0 is generated. I continue try other:
array_push($pipeline, ['$set' => ['todos' => [
'$function' => [
'body' => 'function(todos) {
return todos; // or do something
}',
'args' => ['todos'],
'lang' => 'js',
]
]]]);
But todos field turn into string is "todos", not array. What wrong with $set and $function? And other operators can do the same easier like $map or $filter?
My full pipeline update:
[
{
"$set": {
"name": "First Task 1",
"desc": "This is description",
"status": "completed",
"done": 100,
"level": "medium",
"company": "BBB",
"project": "AAA",
"from_date": "2022/01/01 00:00:00",
"to_date": "2022/01/01 00:00:00",
"updated_at": "$$NOW"
}
},
{
"$set": {
"todos.1.active": false
}
},
{
"$set": {
"todos": {
"$concatArrays": [
"$todos",
[
{
"name": "sub3",
"desc": "desc3",
"active": true,
"owner": "61e6125db0f102060951aa53"
}
]
]
}
}
}
]
db.collection.updateOne(['_id' => ObjectId('')], $pipeline, ['multi' => true]})
I want to update with pipeline, not each single query.
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
{
$set: {
"todos.$[elem].active": false
}
},
{
"multi": true,
"arrayFilters": [
{
"elem.name": {
$eq: "sub1"
}
}
]
})
mongoplayground
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
{
$set: {
"todos.0.active": false,
"todos.2.active": false
}
},
{
"multi": true
})
mongoplayground
db.collection.update({
"_id": "61e7e78372d3221d2c5fb242"
},
[
{
$set: {
todos: {
$map: {
input: {
$range: [ 0, { $size: "$todos" } ]
},
as: "i",
in: {
$let: {
vars: {
"item": { $arrayElemAt: [ "$todos", "$$i" ] }
},
in: {
$cond: {
if: {
$in: [ "$$i", [ 0, 2 ] ]
},
then: {
"name": "$$item.name",
"desc": "$$item.desc",
"active": false,
"owner": "$$item.owner"
},
else: "$$item"
}
}
}
}
}
}
}
}
],
{
"multi": true
})
mongoplayground
I have this array of object:
[
{
"id": 1,
"name": "Carbo",
"menus": [
{
"id": 33,
"name": "FloralWhite",
"image": {
"web": "https://lorempixel.com/640/360/food/?89722",
"mobile": "https://lorempixel.com/640/360/food/?89722",
"square": "https://lorempixel.com/640/360/food/?89722"
},
"logs": {
"price": 2
}
},
{
"id": 40,
"name": "LightGray",
"image": {
"web": "https://lorempixel.com/640/360/food/?63930",
"mobile": "https://lorempixel.com/640/360/food/?63930",
"square": "https://lorempixel.com/640/360/food/?63930"
},
"logs": {
"price": 2
}
},
]
}
]
What I want to achieve:
[
{
"id": 1,
"name": "Carbo",
"menus": [
{
"id": 33,
"name": "FloralWhite",
"image": {
"web": "https://lorempixel.com/640/360/food/?89722",
"mobile": "https://lorempixel.com/640/360/food/?89722",
"square": "https://lorempixel.com/640/360/food/?89722"
},
"price": 2
},
{
"id": 40,
"name": "LightGray",
"image": {
"web": "https://lorempixel.com/640/360/food/?63930",
"mobile": "https://lorempixel.com/640/360/food/?63930",
"square": "https://lorempixel.com/640/360/food/?63930"
},
"price": 2
},
]
}
]
I've tried using laravel collection flatten with depth but it fail
$data = collect($data->menus);
$data = $data->flatten(1);
$data->values()->all();
How can I flatten the menus['logs'] object so it can one level with menu?
Something like this should do the trick. Simply iterate over your array, set the new property and remove the one you don't want.
$data = json_decode("[{
\"id\": 1,
\"name\": \"Carbo\",
\"menus\": [
{
\"id\": 33,
\"name\": \"FloralWhite\",
\"image\": {
\"web\": \"https://lorempixel.com/640/360/food/?89722\",
\"mobile\": \"https://lorempixel.com/640/360/food/?89722\",
\"square\": \"https://lorempixel.com/640/360/food/?89722\"
},
\"logs\": {
\"price\": 2
}
},
{
\"id\": 40,
\"name\": \"LightGray\",
\"image\": {
\"web\": \"https://lorempixel.com/640/360/food/?63930\",
\"mobile\": \"https://lorempixel.com/640/360/food/?63930\",
\"square\": \"https://lorempixel.com/640/360/food/?63930\"
},
\"logs\": {
\"price\": 2
}
}
]
}]");
foreach($data as $val){
foreach($val->menus as $menuVal){
$menuVal->price = $menuVal->logs->price;
unset($menuVal->logs);
}
}
#Stefen Suhat i really don't know what is the syntax for laravel but i did it with a simple php foreach() hope this will help you, as your array's depth is long so i had gone to all the levels of your array, check code and output snippet here https://eval.in/806879
try below one:
<?php
$array = array(
array(
"id"=> 1,
"name"=> "Carbo",
"menus"=> array(
array(
"id"=> 33,
"name"=> "FloralWhite",
"image"=> array(
"web"=> "https=>//lorempixel.com/640/360/food/?89722",
"mobile"=> "https=>//lorempixel.com/640/360/food/?89722",
"square"=> "https=>//lorempixel.com/640/360/food/?89722"
),
"logs"=> array(
"price"=> 2
)
),
array(
"id"=> 40,
"name"=> "LightGray",
"image"=> array(
"web"=> "https=>//lorempixel.com/640/360/food/?63930",
"mobile"=> "https=>//lorempixel.com/640/360/food/?63930",
"square"=> "https=>//lorempixel.com/640/360/food/?63930"
),
"logs"=> array(
"price"=> 2
)
),
)
)
);
foreach($array as $key => $value){
foreach ($value as $key1 => $value1) {
if(is_array($value1) && $key1 == "menus"){
foreach($value1 as $key2 => $value2) {
foreach ($value2 as $key3 => $value3) {
if(is_array($value3) && $key3 == "logs"){
unset($array[$key][$key1][$key2][$key3]);
$array[$key][$key1][$key2] = array_merge($array[$key][$key1][$key2], $value3);
}
}
}
}
}
}
echo "array after<br>";
echo "<pre>";
print_r($array); //your array after
?>
I am trying to get data from a multidimensional array I got from a Facebook response, into a simpler array.
the json data is as seen below.
{
"albums": {
"data": [
{
"photos": {
"data": [
{
"name": "a photo name",
"source": "https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xlp1/t31.0-8/s720x720/XXXXXXXXXXXX",
"picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-0/XXXXXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtp1/v/t1.0-0/p480x480/XXXXXXXXXXXXXXXXXX",
"picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtp1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtf1/v/t1.0-0/p480x480/XXXXXXXXXXXXXX",
"picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtf1/v/t1.0-0/s130x130/XXXXXXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/p480x480/XXXXXXXXXXXXXXXXXX",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
}
],
"paging": {
"cursors": {
"before": "MTXXXXXXXXXXXXXXXZD",
"after": "MXXXXXXXXXXXXXZD"
}
}
},
},
{
"photos": {
"data": [
{
"source": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-9/XXXXXXXXXXXXXXX",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
},
{
"name": "XXXXXXXXX",
"source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/XXXXXXXXXXX",
"picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
},
{
"name": "XXXXXXXXXXX",
"source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/XXXXXXXXXXX",
"picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/p130x130/XXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXXXXX",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xpa1/t31.0-0/XXXXXXXXXXX",
"picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xpt1/v/t1.0-0/XXXXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t31.0-0/p480x480/XXXXXXXXXX",
"picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xat1/v/t1.0-0/s130x130/XXXXXXXX",
},
{
"name": "a photo name",
"source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/v/t1.0-0/s130x130/XXXXXXXX",
}
],
"paging": {
"cursors": {
"before": "MTXXXXXXXXXXXXXXXgZDXXXX",
"after": "MTcXXXXXXXXXXXXXXXXXD"
}
}
},
},
{
"photos": {
"data": [
{
"source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/1",
"picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-0/s130x130/",
},
{
"name": "XXXXXXXXXXXXXXXXX",
"source": "https://scontent.xx.fbcdn.net/v/t1.0-9/11",
"picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-0/s130x130/11",
}
],
"paging": {
"cursors": {
"before": "MOIXXXXXXXXXXXXX",
"after": "MTXXXXXXXXXXXXXD"
}
}
},
},
{
"photos": {
"data": [
{
"source": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpl1/t31.0-8/s720x720/",
"picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xft1/v/t1.0-0/s130x130/",
}
],
"paging": {
"cursors": {
"before": "MTXXXXXXXXXXXSJKSDJZDZD",
"after": "MTdXXXXXXXXDKSJKDNXXXXXXXXXpZD"
}
}
},
}
],
"paging": {
"cursors": {
"before": "MXXXXXXXXXXXXXXD",
"after": "MXXXXXXXXXXXXJR"
}
}
},
"feed": {
"data": [
{
},
{
"attachments": {
"data": [
{
"media": {
"image": {
"height": 480,
"src": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpf1/t31.0-8/s720x720/",
"width": 720
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"media": {
"image": {
"height": 102,
"src": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/",
"width": 197
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"media": {
"image": {
"height": 276,
"src": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpl1/t31.0-8/s720x720/",
"width": 720
}
}
}
]
}
},
{
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXXXXXXKE",
"media": {
"image": {
"height": 540,
"src": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/",
"width": 540
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXXXXJ",
"media": {
"image": {
"height": 375,
"src": "https://scontent.xx.fbcdn.net/v/t1.0-9/",
"width": 500
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXXXXXX",
"media": {
"image": {
"height": 540,
"src": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-0/p180x540/",
"width": 720
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXXXXXXp",
"media": {
"image": {
"height": 405,
"src": "https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/",
"width": 720
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"media": {
"image": {
"height": 392,
"src": "https://scontent.xx.fbcdn.net/v/t1.0-9/",
"width": 626
}
}
}
]
}
},
{
"attachments": {
"data": [
{
"description": "XXXXXXXX",
"media": {
"image": {
"height": 255,
"src": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/",
"width": 208
}
}
}
]
}
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.5/1XXXXXXXXXXXX6/feed?fields=a",
"next": "https://graph.facebook.com/v2.5/1SXXXXXXXXXXXXX96/feed?fields="
}
},
}
I want to end up with an array like this:
Array
(
[photo] => Array
(
[0] => Array
(
[description] => a photo name
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
[1] => Array
(
[description] => a photo name
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
[2] => Array
(
[description] => a photo name
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
and so on......
)
)
But I get this:
Array
(
[photo] => Array
(
[0] => Array
(
[description] => a photo name
)
[1] => Array
(
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
[2] => Array
(
[description] => a photo name
)
[3] => Array
(
[image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
)
and so on...
)
)
This is the code for my current loop:
foreach ($raw_facebook['albums']['data'] as $photos) {
if ($photos) {
foreach ($photos['photos']['data'] as $photo) {
if ( (isset($photo['name']) && array_key_exists('name', $photo)) && (isset($photo['source']) && $this->array_key_exists_recursive('source', $photo)) ) {
$facebook['photo'][]['description'] = $photo['name'];
$facebook['photo'][]['image_src'] = $photo['source'];
}
}
}
}
When I modify this code to this:
foreach ($raw_facebook['albums']['data'] as $photos) {
if ($photos) {
$i = 0;
foreach ($photos['photos']['data'] as $photo) {
if ( (isset($photo['name']) && array_key_exists('name', $photo)) && (isset($photo['source']) && $this->array_key_exists_recursive('source', $photo)) ) {
$facebook['photo'][$i]['description'] = $photo['name'];
$facebook['photo'][$i]['image_src'] = $photo['source'];
}
}
$i++;
}
}
I get a result similar to what I need, but with only 6 inner arrays (when there should be like 10 of them), showing the iteration isn't going correctly.
Please I'd appreciate to be pointed in the right direction.
Did this: array_push($facebook['photo'], array( 'description' => $photo['name'], 'image_src' => $photo['source'] ));
$facebook['photo'] = array();
foreach ($raw_facebook['albums']['data'] as $photos) {
if ($photos){
foreach($photos['photos']['data'] as $photo) {
if((isset($photo['name']) && array_key_exists('name', $photo)) && (isset($photo['source']) && $this->array_key_exists_recursive('source', $photo)) ){
array_push($facebook['photo'], array( 'description' => $photo['name'], 'image_src' => $photo['source'] ));
}
}
}
}
Here you go
$data = $raw_facebook['albums']['data']; //indexed array of objects
$output = array('photo' => array());
for($i = 0; $i< count($data); $i++){
$photosdataarray = $data[$i]['photos']['data'];
for($j = 0; $j < count($photosdataarray); $j++){
$output['photo'][] = array(
'description' => $photosdataarray[$j]['name'],
'image_src' => 'source',
);
}
}
Consider using collection pipeline for this kind of data processing. You'll end up with cleaner code (i.e. no foreach > if > foreach).
Example with your code (using Knapsack):
$photos = Collection::from($raw_facebook['albums']['data'])
->extract('photos.data')
->flatten(1)
->filter(function (array $data) {
return isset($data['name'])
&& isset($data['source'])
&& $this->array_key_exists_recursive('source', $data);
})
->map(function (array $data) {
return [
'description' => $data['name'],
'image_src' => $data['source']
];
})
->values()
->toArray();
Libraries:
https://dusankasan.github.io/Knapsack
https://laravel.com/docs/5.1/collections
https://github.com/cakephp/collection
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.
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;