I'm into a project where I'm trying to get information from a json response (Again), that I got from google+.
I need to return an array after populating it with some information from the json response.
The [modified] json response is thus:
{
"items": [
{
"kind": "plus#activity",
"etag": "\"gLJf7Ldsgfsdgsgsf9ES9mEc/LFcp0tW7Ffgfsdgdf4Yfgdff3Z8\"",
"title": "first title",
"published": "2015-08-13T12:23:36.316Z",
"updated": "2015-08-13T12:23:36.316Z",
"id": "z12njfgogxirwdsoihosdhgjsdghusdn",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/8QZBMRjHVMF",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AAAfgdagAAAI/AAAgdfgfdAPQ/oBXvfgdgdf1aeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": " \u003ca rel=\"nofollow\" class=\"ot-hashtag\" href=\"https://plus.google.com/s/%oko\"\u003es\u003c/a\u003e\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/8QZiUyuyuobsvASKKHVMF",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z12ZiUyuyuobsvASKKcxqnn/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z12njfgogZiUyuyuobsvASKKcxqnn/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z12njfZiUyuyuobsvASKKqnn/people/resharers"
},
"attachments": [
{
"objectType": "photo",
"displayName": "Photo 1",
"id": "210973863758690009009.6618960307328673257603073286732572860914",
"content": "123",
"url": "https://plus.google.com/photos/21096189603073286732572860914009/albums/616189603073286732572860914457/66189603073286732572860914385860914",
"image": {
"url": "https://lh5.googleusercontent.com/-2dgjFRZCII8/VcyMOOCHLTI/BAAHYGHDSJH/0XtjUD1perY/w506-h750/11017878_1655020691394612_1878283374465430482_n.jpg",
"type": "image/jpeg"
},
"fullImage": {
"url": "https://lh5.googleusercontent.com/-2dgjFRSDSDD8/VcyMSDSDSI/BAAHYGHDSJHDSIKA/0XtjIUYDIGIDGerY/11017878_1655020DSDKHSJ612_1878283374SDSDSD30482_n.jpg",
"type": "image/jpeg",
"height": 540,
"width": 540
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJf7LwN3wOpLHXk4IeQ9ES9mEc/TUMFP7Wc8QTGatBg1vGSEbjPPDE\"",
"title": "second title",
"published": "2015-08-13T11:57:49.262Z",
"updated": "2015-08-13T11:57:49.262Z",
"id": "z1iohyfsdugsjhockob0n232vjiz5xancxqnn",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/4DVBTreNkyA",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/BAAHYGHDSJH/BAAHYGHDSJH/oBXvDSDASAeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "07012345678\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/4DVBiodjjdmknaayA",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/pfjknsfubsnjbsb0n232vjiz5xancxqnn/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/pfjknsfubsnjbsb0n232vjiz5xancxqnn/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/pfjknsfubsnjbsb0n232vjiz5xancxqnn/people/resharers"
},
"attachments": [
{
"objectType": "photo",
"displayName": "Photo 2",
"id": "210973863758690009009.6837572367923789379",
"content": "456",
"url": "https://plus.google.com/photos/210973863758690009009/albums/6947862082892799114865/69843868408496745035842",
"image": {
"url": "https://lh3.googleusercontent.com/-Tvib82iMpsk/VcyGKh3UGAI/ADLJSKNSHJBSDNB AAQM/cyhYX3SifDY/w506-h750/1024-2006_1011_093752.jpg",
"type": "image/jpeg"
},
"fullImage": {
"url": "https://lh3.googleusercontent.com/-Tvijihjsdbnjgsdupsk/VcfjhjfsbjgsugAI/ApkdlnsdjkbjsdAQM/cyhYX3SifDY/w1024-h768/1024-2006_1011_093752.jpg",
"type": "image/jpeg",
"height": 768,
"width": 1024
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJfnjsdjnvdsvhsdIeQ9EpoifkjnfmEc/P4ihjsadiadakhlbhvdshvfXan4\"",
"title": "third title",
"published": "2015-08-12T22:57:22.010Z",
"updated": "2015-08-12T22:57:22.010Z",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/iiuhfdjhfidpnbvcW",
"actor": {
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/BAJDHJDSIDKAI/PODSIOUSDIKAAPQ/oBXvlp1aeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": " \u003ca rel=\"nofollow\" class=\"ot-hashtag\" href=\"https://plus.google.com/s/IUYDHJDSUGDSHGY\"\u003eIUYDHJDSUGDSHGY\u003c/a\u003e \ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/iukdjkdowqvcW",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/uihfjbsjkbsfdnfffdfdpokrouh3quig/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/uihfjbsjkbsfdnfffdfdpokrouh3quig/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/uihfjbsjkbsfdnfffdfdpokrouh3quig/people/resharers"
},
"attachments": [
{
"objectType": "photo",
"displayName": "Photo 3",
"content": "789",
"url": "https://plus.google.com/photos/210973863758690009009/albums/6182262671805531297/618654563857564534922",
"image": {
"url": "https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/X6J9fHJVBFDVSFVSak/w506-h750/PicsArt_1438914428134.jpg",
"type": "image/jpeg"
},
"fullImage": {
"url": "https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/XyghFGF2TgBak/w562-h562/PicsArt_1438914428134.jpg",
"type": "image/jpeg",
"height": 562,
"width": 562
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJioyuhgdugudsgugc/UGHAoodpuskhjhgsdjdbY0\"",
"title": "fourth title",
"published": "2011-09-26T17:13:14.309Z",
"updated": "2011-09-26T17:13:14.309Z",
"id": "z12odlnbgmavzdh4k04cgfhxvwrouh3quig",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/R9zieiuhdjghdgBFu",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AKHDSJHSDGAAI/AUIDHIJGDSHGHAPQ/oBXvlp1aeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "UIHUIGG FJGF GU",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/R9zStuw9BFu",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/comments"
},
"plusoners": {
"totalItems": 1,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/zkjhsdsfgiooi;oilzdh4k04cgpooiyuewfouh3quig/people/resharers"
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"oilzdh4k04cgpooiyuewfouh3quig/oilzdh4k04cgpooiyuewfouh3quig\"",
"title": "fifth title",
"published": "2011-09-26T17:09:23.712Z",
"updated": "2011-09-26T17:09:23.712Z",
"id": "z13msxkxfnjjvp04p04cgfhxvwrouh3quig",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/RvXF5ufy23n",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AAAAAAAAAAI/AAAAAAAAAPQ/oilzdh4k04cgpooiyuewfouh3quig/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "witout 'ME' it's jst AWESO....\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/RvXF5ufy23n",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/people/resharers"
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJfoiojkdwjkshdikhdhisEc/gLJfoiojkdwjkshdikhdhisEc\"",
"title": "sixth title",
"published": "2011-09-26T17:07:05.897Z",
"updated": "2011-09-26T17:07:05.897Z",
"id": "gLJfoiojkdwjkshdikhdhisEc",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/gLJfoiojkdwjkshdikhdhisEc",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AASKJAKHJGASQAPQ/AASKJAKHJGASQAPQ/gLJfoiojkdwjkshdikhdhisEc/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "dere's an xception 2 evry rule, xcept dis 1..\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/N6QTZt2XvSR",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/people/resharers"
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
}
]
}
this is what I'm doing:
function google_pluss() {
$file_dir = strtolower(realpath(APPPATH . '..' . DIRECTORY_SEPARATOR . 'assets'));
$json_file = $file_dir . DIRECTORY_SEPARATOR . 'stackoverflow' . '.json'; // I have the file locally...
$json_data = json_decode(file_get_contents($json_file));
$data = array();
if (isset($json_data)) {
$x = 0;
foreach ($json_data->items as $item) {
if (isset($item->object->attachments)) {
foreach ($item->object->attachments as $attachment) {
$data['number'][$x] = $attachment->content;
$data['image'][$x] = $attachment->fullImage->url;
}
$data['title'][$x] = $item->title;
$x++;
}
}
}
print_r($data);
}
this is the output I get:
Array
(
[number] => Array
(
[0] => 123
[1] => 456
[2] => 789
)
[image] => Array
(
[0] => https://lh5.googleusercontent.com/-2dgjFRSDSDD8/VcyMSDSDSI/BAAHYGHDSJHDSIKA/0XtjIUYDIGIDGerY/11017878_1655020DSDKHSJ612_1878283374SDSDSD30482_n.jpg
[1] => https://lh3.googleusercontent.com/-Tvijihjsdbnjgsdupsk/VcfjhjfsbjgsugAI/ApkdlnsdjkbjsdAQM/cyhYX3SifDY/w1024-h768/1024-2006_1011_093752.jpg
[2] => https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/XyghFGF2TgBak/w562-h562/PicsArt_1438914428134.jpg
)
[title] => Array
(
[0] => first title
[1] => second title
[2] => third title
)
)
But, this is the output I actually need:
Array
(
[0] => Array
(
[number] => 123
[image] => https://lh5.googleusercontent.com/-2dgjFRSDSDD8/VcyMSDSDSI/BAAHYGHDSJHDSIKA/0XtjIUYDIGIDGerY/11017878_1655020DSDKHSJ612_1878283374SDSDSD30482_n.jpg
[title] => first title
)
[1] => Array
(
[number] => 456
[image] => https://lh3.googleusercontent.com/-Tvijihjsdbnjgsdupsk/VcfjhjfsbjgsugAI/ApkdlnsdjkbjsdAQM/cyhYX3SifDY/w1024-h768/1024-2006_1011_093752.jpg
[title] => second title
)
[2] => Array
(
[number] => 789
[image] => https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/XyghFGF2TgBak/w562-h562/PicsArt_1438914428134.jpg
[title] => third title
)
)
I'd really appreciate if someone shows me how to achieve this, thank you.
btw, I'm using the $x variable and incrementing it, to stop the loop where the array doesn't contain attachments. I'd be glad too, if shown how to do this neater.
At first glance, I would say change
foreach ($item->object->attachments as $attachment) {
$data['number'][$x] = $attachment->content;
$data['image'][$x] = $attachment->fullImage->url;
}
to
foreach ($item->object->attachments as $attachment) {
$data[$x]['number'] = $attachment->content;
$data[$x]['image'] = $attachment->fullImage->url;
}
Related
I've been trying to build an index in ES and add the initial items to it (around 350k), using PHP.
I tried all kinds of batch sizes (from 10 items to 1k), check the count, check the threshold, but for some reason it doesn't index every item.
It just skips over some random items, without any errors in the batch result response. I feel like I tried everything and I have to idea what to do next
I'm using Amazon OpenSearch with the latest supported ES (7.10).
The index looks like this:
{
"wonder-search": {
"aliases": {},
"mappings": {
"properties": {
"address": {
"type": "text"
},
"city": {
"type": "text"
},
"city_id": {
"type": "integer"
},
"duration": {
"type": "integer"
},
"filename": {
"type": "text"
},
"geo_point": {
"type": "geo_point"
},
"icon": {
"type": "keyword"
},
"is_sandbox": {
"type": "integer"
},
"item_id": {
"type": "integer"
},
"item_label": {
"type": "keyword"
},
"latitude": {
"type": "float"
},
"longitude": {
"type": "float"
},
"search_text_caption_json": {
"type": "text",
"index_phrases": true
},
"search_text_city_json": {
"type": "text",
"index_phrases": true
},
"search_text_completion": {
"type": "completion",
"analyzer": "simple",
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"contexts": [
{
"name": "type",
"type": "CATEGORY"
}
]
},
"search_text_country_json": {
"type": "text",
"index_phrases": true
},
"search_text_cuisine_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_location_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_state_json": {
"type": "text",
"index_phrases": true
},
"search_text_tag_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_username_json": {
"type": "text",
"index_phrases": true
},
"sort": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sort_score": {
"type": "double"
},
"type": {
"type": "text"
},
"user_icon": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_id": {
"type": "integer"
},
"username": {
"type": "keyword"
},
"vanity_url": {
"type": "keyword"
},
"video_count": {
"type": "integer"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"mapping": {
"ignore_malformed": "true"
},
"number_of_shards": "1",
"provided_name": "wonder-search",
"creation_date": "1671003076106",
"number_of_replicas": "1",
"uuid": "YQh1q40WTneLE4MWDWhArw",
"version": {
"created": "7100199"
}
}
}
}
}
and one item looks like this:
{
"_index": "wonder-search",
"_type": "_doc",
"_id": "wq2LD4UBUAuy7FQPhtZh",
"_version": 1,
"_seq_no": 2003,
"_primary_term": 1,
"found": true,
"_source": {
"sort": "4004",
"item_id": "4934",
"user_id": "434",
"user_icon": "/site-content/avatars/Sp8AXjTJvMbRao2oZbuiUuSVH042-1597776099045.jpeg",
"username": "chuurros",
"item_label": "Kyoto Katsugyu【京都勝牛】",
"search_text_username_json": [
"chuurros"
],
"search_text_caption_json": [
"Absolutely love their gyukatsu (beef katsu) here! Delicious and will keep you wanting more! 🥰"
],
"search_text_city_json": [
"Toronto"
],
"search_text_state_json": [
"Ontario"
],
"search_text_country_json": [
"Canada"
],
"search_text_location_name_json": [
"Kyoto Katsugyu【京都勝牛】"
],
"search_text_tag_name_json": [
"japanese",
"restaurant",
"asian",
"dining",
"topcollection-4934"
],
"search_text_cuisine_name_json": [],
"type": "video",
"vanity_url": "",
"icon": "",
"city": "Toronto",
"city_id": "439",
"latitude": "43.65682410",
"longitude": "-79.37617410",
"address": "134 Dundas St E",
"duration": "9.57",
"video_count": "0",
"sort_score": "43",
"filename": "373d75fd-4292-4e5b-a239-4b1c39ffc86c.MOV",
"is_sandbox": "0",
"geo_point": {
"lat": "43.65682410",
"lon": "-79.37617410"
},
"search_text_completion": {
"input": [
"Kyoto Katsugyu【京都勝牛】"
],
"contexts": {
"type": [
"video"
]
}
}
}
}
Any ideas why does it work like this?
I have a list of (the result of a query in DB) like this:
[
{
"id": "1",
"parent_id": null,
"title": "مدادنوکی",
"url": "/medadnoki"
},
{
"id": "2",
"parent_id": null,
"title": "جامعه",
"url": "/commiunity"
},
{
"id": "5",
"parent_id": "1",
"title": "درباره ی مدادنوکی",
"url": "/about"
},
{
"id": "6",
"parent_id": "1",
"title": "درباره ی مدادنوکی",
"url": "/about"
},
{
"id": "7",
"parent_id": "2",
"title": "همکاران",
"url": "/co-worker"
},
{
"id": "8",
"parent_id": "2",
"title": "اساتید",
"url": "/masters"
}
]
But I want to create an object like this:
[
{
"title": "مدادنوکی",
"url": "/medadnoki",
"subs" : [
{
"title": "درباره مدادنوکی",
"url": "/about"
},
{
"title": "درباره مدادنوکی",
"url": "/about"
},
{
"title": "درباره مدادنوکی",
"url": "/about"
},
{
"title": "درباره مدادنوکی",
"url": "/about"
}
]
},
{
"title": "جامعه",
"url": "/soc",
"subs" : [
{
"title": "همکاران",
"url": "/co-work"
},
{
"title": "اساتید",
"url": "/masters"
}
]
}
]
I have handled this process with two foreach in php and it means I process data twice and it is not efficient and will be slow.
Is there any Idea to doing this with just one foreach?
using one foreach means faster than two foreach twice and It will show the result in big data
Assuming you have the results in an order where the parents appear in the results before any children they may have, this should work:
$nested = [];
foreach($results as $r) {
if($r['parent_id'] === null) {
$nested[$r['id']] = [
'title' => $r['title'],
'url' => $r['url'],
'subs' => []
];
continue;
}
$nested[$r['parent_id']]['subs'][] = [
'title' => $r['title'],
'url' => $r['url']
];
}
$nested = array_values($nested);
I'm trying to display a JSON Array in php
JSON:
"members":[
{
"username":"Abstract",
"status":"online",
"bot":true,
"nick":"ViralBot \ud83d\ude0b",
"avatar_url":"https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar":"59b492151d2c352f3ac5e249e4f04a82",
"discriminator":"0612",
"id":"133718676741292033"
},
{
"username":"Aethex",
"status":"online",
"bot":true,
"game":{
"name":"aethex.xyz | -help"
},
"avatar_url":"https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar":"bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator":"0394",
"id":"150300454708838401"
},
{
"username":"AIRHORN SOLUTIONS",
"status":"online",
"bot":true,
"nick":"MLG AIRHORNZ!",
"game":{
"name":"airhornbot.com"
},
"avatar_url":"https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar":"96be9927ca947a75021db568485612fc",
"discriminator":"6723",
"id":"159800228088774656"
},
{
"username":"BuddyGang",
"status":"online",
"game":{
"name":"Custom Widgets API"
},
"avatar_url":"https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar":"089e84734ea5ca86d20292abcca0982f",
"discriminator":"1704",
"id":"97172171259904000"
},
{
"username":"danclay",
"status":"dnd",
"nick":"danclay \ud83d\ude09",
"avatar_url":"https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar":"fa460980203d7838c8481c93c0361f6d",
"discriminator":"2051",
"id":"97147476531757056"
},
{
"username":"dragongod100",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar":"5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator":"5291",
"id":"96716994136514560"
},
{
"username":"Hax0nWax0ff",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar":"ccf68e818fa44e342f60030a89a0fd53",
"discriminator":"7691",
"id":"125738028793593856"
},
{
"username":"iiPsionic",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar":"ba4bba83436f227933004694b4c184f2",
"discriminator":"6034",
"id":"142014347038818304"
},
{
"username":"RH1-N0",
"status":"online",
"bot":true,
"nick":"RHINO ATTACK!",
"avatar_url":"https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar":"fe0070998d6c9378edf8243e3267f950",
"discriminator":"6993",
"id":"135288293548883969"
},
{
"username":"WildBot",
"status":"online",
"bot":true,
"nick":"Cra-Cra Bot \ud83d\ude31",
"avatar_url":"https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar":"f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator":"3942",
"id":"110462073074388992"
}
]
I want to be able to display it in numeric order of the "position" variable, with the avatar positioned next to the username then the game text.
HTML layout:
<img src="AVATAR_URL" width="50%"> USERNAME - <small>GAME</small><br>
try this code bro, hope it helps
$arr=json_decode($data,true);
// to prevent error undefined index
if(isset($arr['members'])) {
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}
}
Here the code:-
$data='{
"members": [
{
"username": "Abstract",
"status": "online",
"bot": true,
"nick": "ViralBot 😋",
"avatar_url": "https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar": "59b492151d2c352f3ac5e249e4f04a82",
"discriminator": "0612",
"id": "133718676741292033"
},
{
"username": "Aethex",
"status": "online",
"bot": true,
"game": {
"name": "aethex.xyz | -help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar": "bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator": "0394",
"id": "150300454708838401"
},
{
"username": "AIRHORN SOLUTIONS",
"status": "online",
"bot": true,
"nick": "MLG AIRHORNZ!",
"game": {
"name": "airhornbot.com"
},
"avatar_url": "https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar": "96be9927ca947a75021db568485612fc",
"discriminator": "6723",
"id": "159800228088774656"
},
{
"username": "BuddyGang",
"status": "online",
"game": {
"name": "Custom Widgets API"
},
"avatar_url": "https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar": "089e84734ea5ca86d20292abcca0982f",
"discriminator": "1704",
"id": "97172171259904000"
},
{
"username": "danclay",
"status": "dnd",
"nick": "danclay 😉",
"avatar_url": "https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar": "fa460980203d7838c8481c93c0361f6d",
"discriminator": "2051",
"id": "97147476531757056"
},
{
"username": "dragongod100",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar": "5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator": "5291",
"id": "96716994136514560"
},
{
"username": "Hax0nWax0ff",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar": "ccf68e818fa44e342f60030a89a0fd53",
"discriminator": "7691",
"id": "125738028793593856"
},
{
"username": "iiPsionic",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar": "ba4bba83436f227933004694b4c184f2",
"discriminator": "6034",
"id": "142014347038818304"
},
{
"username": "RH1-N0",
"status": "online",
"bot": true,
"nick": "RHINO ATTACK!",
"avatar_url": "https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar": "fe0070998d6c9378edf8243e3267f950",
"discriminator": "6993",
"id": "135288293548883969"
},
{
"username": "WildBot",
"status": "online",
"bot": true,
"nick": "Cra-Cra Bot 😱",
"avatar_url": "https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar": "f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator": "3942",
"id": "110462073074388992"
}
]
}';
$arr=json_decode($data,true);
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}
I want to get title for each song from the json below in php.I am unable to get all the names .I tried with the code below but its not displaying anything.I really appreciate any help.Thanks in Advance.
code:
$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json');
$data=(json_decode($json, true));
foreach ($data as $item)
{
foreach ($item->entry as $asset)
{
echo $asset->title;
}
}
json:
{
"feed": {
"author": {
"name": {
"label": "iTunes Store"
},
"uri": {
"label": "http://www.apple.com/itunes/"
}
},
"entry": [
{
"im:name": {
"label": "Happy (From \"Despicable Me 2\")"
},
"im:image": [
{
"label": "http://a923.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a1645.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1228.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "G I R L"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/g-i-r-l/id823593445?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2014 Columbia Records, a Division of Sony Music Entertainment, 2013 Back Lot Music, under exclusive license to Columbia Records, a Division of Sony Music Entertainment"
},
"title": {
"label": "Happy (From \"Despicable Me 2\") - Pharrell Williams"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a1083.phobos.apple.com/us/r1000/014/Music/v4/4e/44/b7/4e44b7dc-aaa2-c63b-fb38-88e1635b5b29/mzaf_1844128138535731917.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2",
"attributes": {
"im:id": "823593456"
}
},
"im:artist": {
"label": "Pharrell Williams",
"attributes": {
"href": "https://itunes.apple.com/us/artist/pharrell-williams/id14934728?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2014-03-03T00:00:00-07:00",
"attributes": {
"label": "March 3, 2014"
}
}
},
{
"im:name": {
"label": "All of Me"
},
"im:image": [
{
"label": "http://a1221.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a1943.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a982.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Love in the Future (Deluxe Edition)"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/love-in-future-deluxe-edition/id679297685?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Getting Out Our Dreams and Columbia Records, a Division of Sony Music Entertainment"
},
"title": {
"label": "All of Me - John Legend"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a355.phobos.apple.com/us/r1000/041/Music4/v4/90/cf/04/90cf0482-07c0-fca0-549f-c1ea62c4bdef/mzaf_6715619947923767616.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2",
"attributes": {
"im:id": "679297849"
}
},
"im:artist": {
"label": "John Legend",
"attributes": {
"href": "https://itunes.apple.com/us/artist/john-legend/id16586443?uo=2"
}
},
"category": {
"attributes": {
"im:id": "15",
"term": "R&B/Soul",
"scheme": "https://itunes.apple.com/us/genre/music-r-b-soul/id15?uo=2",
"label": "R&B/Soul"
}
},
"im:releaseDate": {
"label": "2013-08-30T00:00:00-07:00",
"attributes": {
"label": "August 30, 2013"
}
}
},
{
"im:name": {
"label": "Let It Go"
},
"im:image": [
{
"label": "http://a261.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a983.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a262.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Frozen (Deluxe Edition) [Original Motion Picture Soundtrack]"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/frozen-deluxe-edition-original/id728903889?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Walt Disney Records"
},
"title": {
"label": "Let It Go - Idina Menzel"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a878.phobos.apple.com/us/r1000/023/Music/v4/0a/eb/c2/0aebc299-44e3-3465-3cdf-5bab336b547a/mzaf_7970981567593232494.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2",
"attributes": {
"im:id": "728904000"
}
},
"im:artist": {
"label": "Idina Menzel",
"attributes": {
"href": "https://itunes.apple.com/us/artist/idina-menzel/id3297504?uo=2"
}
},
"category": {
"attributes": {
"im:id": "16",
"term": "Soundtrack",
"scheme": "https://itunes.apple.com/us/genre/music-soundtrack/id16?uo=2",
"label": "Soundtrack"
}
},
"im:releaseDate": {
"label": "2013-11-25T00:00:00-07:00",
"attributes": {
"label": "November 25, 2013"
}
}
},
{
"im:name": {
"label": "Dark Horse (feat. Juicy J)"
},
"im:image": [
{
"label": "http://a1657.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a731.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1498.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "PRISM (Deluxe Version)"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/prism-deluxe-version/id690928033?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Capitol Records, LLC"
},
"title": {
"label": "Dark Horse (feat. Juicy J) - Katy Perry"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a1930.phobos.apple.com/us/r1000/022/Music/v4/a4/ac/63/a4ac6341-59e3-bcfe-b7f5-370100eea063/mzaf_4835051646631223146.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2",
"attributes": {
"im:id": "690928460"
}
},
"im:artist": {
"label": "Katy Perry",
"attributes": {
"href": "https://itunes.apple.com/us/artist/katy-perry/id64387566?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2013-10-22T00:00:00-07:00",
"attributes": {
"label": "October 22, 2013"
}
}
},
{
"im:name": {
"label": "The Man"
},
"im:image": [
{
"label": "http://a1169.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a243.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1234.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Lift Your Spirit"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/lift-your-spirit/id798928288?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2014 Aloe Blacc Recording, Inc. under exclusive license to XIX Recordings LLC/Interscope Records"
},
"title": {
"label": "The Man - Aloe Blacc"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a85.phobos.apple.com/us/r1000/033/Music6/v4/8a/09/dc/8a09dcf0-4845-81ae-6936-a94972ad94e7/mzaf_5332424013433716082.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2",
"attributes": {
"im:id": "798928362"
}
},
"im:artist": {
"label": "Aloe Blacc",
"attributes": {
"href": "https://itunes.apple.com/us/artist/aloe-blacc/id4750752?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2014-03-11T00:00:00-07:00",
"attributes": {
"label": "March 11, 2014"
}
}
}
],
"updated": {
"label": "2014-03-26T14:09:41-07:00"
},
"rights": {
"label": "Copyright 2008 Apple Inc."
},
"title": {
"label": "iTunes Store: Top Songs"
},
"icon": {
"label": "http://itunes.apple.com/favicon.ico"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?cc=us&id=38&popId=1"
}
},
{
"attributes": {
"rel": "self",
"href": "http://itunes.apple.com/us/rss/topsongs/limit=5/json"
}
}
],
"id": {
"label": "http://itunes.apple.com/us/rss/topsongs/limit=5/json"
}
}
}
$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json');
$data=json_decode($json);
$titles = array();
foreach ($data as $item)
{
foreach ($item->entry as $asset)
{
$titles[] = $asset->title->label;
}
}
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.