Below is the result and I want to remove duplicate from the array
I tried using this code: $login_data1['items'] = array_values(array_map("unserialize", array_unique(array_map("serialize", $login_data1['items']))));
{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual",
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
],
}
I tried using $login_data1['items'] = array_unique($login_data1['items'] ,SORT_REGULAR); but this adds serial numbers at the each json response
Try as using array_unique
$json = '{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
]
}';
foreach(json_decode($json, true) as $key => $value){
$input = array_unique($value,SORT_REGULAR);
}
If its an array then simply use
array_unique($login_data['items'],SORT_REGULAR);
Fiddle
array_unique works perfectly if you pass a multidimensional array.
$login_data1['items'] = array_unique($login_data1['items'], SORT_REGULAR);
It doesn't work with your json because it's an array of object. Infact:
$array = json_decode($json);
var_dump($array);
returns:
object(stdClass)#1 (1) {
["items"]=>
array(3) {
[0]=>
object(stdClass)#2 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#3 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
[1]=>
object(stdClass)#4 (3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#5 (1) {
["name"]=>
string(12) "Snow Leopard"
}
}
["type"]=>
string(3) "faq"
}
[2]=>
object(stdClass)#6 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#7 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
}
}
Your json should look like this:
{
"items": [
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "1",
"tags": {
"name": "Snow Leopard"
},
"type": "faq"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
}
]
}
And now:
$array = json_decode($json);
var_dump($array);
returns:
array(1) {
["items"]=>
array(4) {
[0]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[1]=>
array(3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
["name"]=>
string(12) "Snow Leopard"
}
["type"]=>
string(3) "faq"
}
[2]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[3]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
}
}
And array_unique works.
I got the solution for this.
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));
Try array_value and array_unique together and serial number will be removed!
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));
Related
I'm having trouble sorting alphabetically an object returned by the json_decode function in PHP.
The object I've got is as follows:
object(stdClass)#9263 (1) {
["Activities"]=>
array(91) {
[0]=>
object(stdClass)#6116 (3) {
["Code"]=>
string(1) "5"
["NameFr"]=>
string(16) "Droit ju"
["NameNl"]=>
string(18) "Ger recht"
}
[1]=>
object(stdClass)#8582 (3) {
["Code"]=>
string(1) "1"
["NameFr"]=>
string(19) "Droit per"
["NameNl"]=>
string(13) "Pers recht"
}
[2]=>
object(stdClass)#8598 (3) {
["Code"]=>
string(5) "2"
["NameFr"]=>
string(11) "Droit ca"
["NameNl"]=>
string(14) "Ca recht"
}
...
I need to sort it using the "NameFr" property, alphabetically.
try usort
<?php
$data = '
{
"Activities": [
{
"Code": "5",
"NameFr": "Droit ju",
"NameNl": "Ger recht"
},
{
"Code": "1",
"NameFr": "Droit per",
"NameNl": "Pers recht"
},
{
"Code": "2",
"NameFr": "Droit ca",
"NameNl": "Ca recht"
}
]
}';
$data = json_decode($data, true);
usort($data["Activities"], function($a, $b) {
return $a['NameFr'] <=> $b['NameFr'];
});
print_r($data);
I am currently stuck in json formatting for php. I have given my outputted json below. What I need to do is to make the format of the current json to the desired one. I am missing the arrays in the JSON format. Can anyone help me on this.
My code to print the json output is below:
$menuHead=array();
$i=0;
foreach($res as $key => $value){
$i=$key+1;
//$menuHead[$i]['menuHead']=$value['category'];
if(isset($menuHead[$key]['menuHead'])){
if($menuHead[$key]['menuHead']==$value['category']){
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$key]['data'][$i]['price']=$value['price'];
$menuHead[$key]['data'][$i]['description']=$value['description'];
$menuHead[$key]['data'][$i]['itemId']=$value['id'];
$menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}
$final['MenuList']=$menuHead;
echo json_encode($final);
Current format:
{
"MenuList": {
"1": {
"menuHead": "Main Course",
"data": {
"1": {
"itemName": "Chicken Thai Curry",
"price": "599",
"description": "",
"itemId": "67",
"customizable": "1"
}
}
},
"2": {
"menuHead": "Refreshments",
"data": {
"2": {
"itemName": "Kingfisher Premium",
"price": "999",
"description": "Kingfisher beer",
"itemId": "69",
"customizable": "1"
},
"3": {
"itemName": "Mocktail",
"price": "999",
"description": "",
"itemId": "68",
"customizable": "1"
}
}
},
"4": {
"menuHead": "Rice biriyani",
"data": {
"4": {
"itemName": "Dal makni risotto",
"price": "499",
"description": "Dal makhni risotto",
"itemId": "66",
"customizable": "1"
}
}
}
}
}
Desired Format:
{
"menuList": [
{
"menuHead": "In Steamer (Momos)",
"data": [
{
"itemName": "Tandoori Momo",
"description": "",
"price": "150",
"itemId": "16",
"customizable": "0"
},
{
"itemName": "Fried Momo Pork",
"price": "100",
"description": "",
"itemId": "15",
"customizable": "0"
}
]
},
{
"itemName": "Rice and Noodles",
"data": [
{
"sub_category": "Tandoori Momo",
"description": "",
"price": "150",
"itemId": "16",
"customizable": "0"
},
{
"itemName": "Fried Momo Pork",
"price": "100",
"description": "",
"itemId": "15",
"customizable": "0"
}
]
}
]
}
Raw response is below:
array(4) { [0]=> array(7) { ["id"]=> string(2) "67" ["restaurant_id"]=> string(1) "5" ["category"]=> string(11) "Main Course" ["sub_category"]=> string(18) "Chicken Thai Curry" ["price"]=> string(3) "599" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [1]=> array(7) { ["id"]=> string(2) "69" ["restaurant_id"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["sub_category"]=> string(18) "Kingfisher Premium" ["price"]=> string(3) "999" ["description"]=> string(15) "Kingfisher beer" ["customizable"]=> string(1) "1" } [2]=> array(7) { ["id"]=> string(2) "68" ["restaurant_id"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["sub_category"]=> string(8) "Mocktail" ["price"]=> string(3) "999" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [3]=> array(7) { ["id"]=> string(2) "66" ["restaurant_id"]=> string(1) "5" ["category"]=> string(13) "Rice biriyani" ["sub_category"]=> string(17) "Dal makni risotto" ["price"]=> string(3) "499" ["description"]=> string(18) "Dal makhni risotto" ["customizable"]=> string(1) "1" } }
If you want a javascript compatible array, the index must start at 0. The easiest way to do that, is to use array_values():
$final['MenuList'] = array_values($menuHead);
The problem is that when your adding the data items, you need to add them without specific keys, as you add them with $i as in...
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
This will stop them being a normal array as you want it to be. For json_encode() an array must start at 0 and be sequential for it to be an array.
Instead create them in one go and add them to the end of the existing data using []...
$menuHead[$key]['data'][] = ['itemName' =>$value['sub_category'],
'price'=> $value['price'],
'description'=>$value['description'],
'itemId'=>$value['id'],
'customizable'=>$value['customizable']];
This needs to be done with each set of similar code, which includes the overall array itself, this can be done using
$final['MenuList'] = array_values($menuHead);
To try and fix the data you already have, which means no changes except adding the following code...
foreach ( $menuHead as $menu ) {
$menu['data'] = array_values($menu['data']);
}
$final['MenuList'] = array_values($menuHead);
Use array_values();
I fixed your code, it should work
$menuHead=array();
$i=0;
foreach($res as $key => $value){
$i=$key+1;
//$menuHead[$i]['menuHead']=$value['category'];
if(isset($menuHead[$key]['menuHead'])){
if($menuHead[$key]['menuHead']==$value['category']){
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$key]['data'][$i]['price']=$value['price'];
$menuHead[$key]['data'][$i]['description']=$value['description'];
$menuHead[$key]['data'][$i]['itemId']=$value['id'];
$menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}
// i'am use array_values()
$final['MenuList']= array_values($menuHead);
echo json_encode($final);
just tried several $result->key ($customer->transaction->status) but was not successful. i need the "status" value of this object in php. tried some combinations of array shift, ->object ->["status"] etc.
object(stdClass)#2 (3) {
["customer"]=>
object(stdClass)#3 (1) {
["link"]=>
object(stdClass)#4 (3) {
["url"]=>
string(78) "https://demo1.com"
["rel"]=>
string(8) "customer"
["method"]=>
string(3) "GET"
}
}
["transaction"]=>
object(stdClass)#5 (9) {
["merchantRefId"]=>
string(19) "46532156465456"
["amount"]=>
int(200)
["currency"]=>
string(3) "EUR"
["id"]=>
string(15) "646544564564"
["transactionType"]=>
string(27) "Transfer"
["createDate"]=>
string(19) "2016-01-26 08:33:09"
["updateDate"]=>
string(19) "2016-01-26 08:33:09"
["status"]=>
string(8) "accepted"
["fees"]=>
array(1) {
[0]=>
object(stdClass)#6 (3) {
["feeType"]=>
string(11) "service_fee"
["feeAmount"]=>
int(119)
["feeCurrency"]=>
string(3) "EUR"
}
}
}
["links"]=>
array(1) {
[0]=>
object(stdClass)#7 (3) {
["url"]=>
string(78) "https://demo.com"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
}
Since you didn't provide the code that creates your object, I've created JSON string and converted it to object, which gives similar var_dump result to yours.
<?php
$jsonStr = '{
"customer": {
"link": {
"url": "https://demo1.com",
"rel": "customer",
"method": "GET"
}
},
"transaction": {
"merchantRefId": "46532156465456",
"amount": 200,
"currency": "EUR",
"id": "646544564564",
"transactionType": "Transfer",
"createDate": "2016-01-26 08:33:09",
"updateDate": "2016-01-26 08:33:09",
"status": "accepted",
"fees": [
{
"feeType": "service_fee",
"feeAmount": 119,
"feeCurrency": "EUR"
}
]
},
"links": [
{
"url": "https://demo.com",
"rel": "self",
"method": "GET"
}
]
}';
$stdObj = json_decode($jsonStr);
var_dump($stdObj);
var_dump($stdObj->transaction->status);
I am able, and you should be too, to simply get status with simple:
$customer->transaction->status
I am working on an application where i am generating JSON using PHP. My code is here.
$arrayItem = array_slice($data, $start, $max);
$jsonArray = array();
$jsonArray['total'] = count($arrayItem);
for ($i = 0; $i <= 9; $i ++) {
// Listomatic requires the "total" field to show/hide the "Show More" button
// The following is sample data (in this case Powerball numbers) that you want to display
$item = array(
'id' => $arrayItem[$i]['id'],
'title' => $arrayItem[$i]['title'],
'featured_image' => $arrayItem[$i]['featured_image'],
'audio_file' => $arrayItem[$i]['audio_file'],
'youtube_id' => $arrayItem[$i]['youtube_id'],
'open_par' => $arrayItem[$i]['open_par'],
'category' => $arrayItem[$i]['category']);
$list_array[] = array('item' =>$item);
}
$jsonArray['items'] = $list_array;
header('Content-type: application/json');
echo json_encode($jsonArray);
It produces the following JSON. I am not sure why the zeros are being added.
{
"total": 10,
"items": [
{
"item": {
"id": "234",
"title": {
"0": "Indulged in any Good Vices Lately?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/234_Indulge_in_any_good_vices_lately_12_15_02.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Social Issues"
}
}
},
{
"item": {
"id": "233",
"title": {
"0": "Do You Use the Spirit of a Sound Mind?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/233_Do_you_use_the_spirit_of_a_sound_mind_12_08_02.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Parable"
}
}
},
{
"item": {
"id": "232",
"title": {
"0": "What Does it Mean to be Thankful?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/232_What_does_it_mean_to_be_thankful_12_01_02.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Inspiration"
}
}
},
{
"item": {
"id": "226",
"title": {
"0": "Who is Jesus?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "216",
"title": {
"0": "Does God Really Torture People in Hellfire?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/216_Does_God_really_torture_Hellfire.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "192",
"title": {
"0": "Why Don’t You Ever Listen to Me?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/192_Why_dont_you_ever_listen_to_me_02_10_02.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Character"
}
}
},
{
"item": {
"id": "188",
"title": {
"0": "Who Ends Up in the “Lake of Fire?”"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2002mp3/188_Who_ends_up_in_the_lake_of_fire.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "156",
"title": {
"0": "Is Jesus Really God? (Parts III & IV)"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2001mp3/156_Is_Jesus_really_God.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "155",
"title": {
"0": "Is Jesus Really God? (Parts I & II)"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2001mp3/155_Is_Jesus_really_God.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
},
{
"item": {
"id": "124",
"title": {
"0": "Did God Really Create the World in 6 Days?"
},
"featured_image": {
"0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
},
"audio_file": {
"0": "http://www.christianquestions.net/en/2000mp3/124_Did_God_create_the_world_in_6_days_10_22_00.mp3"
},
"youtube_id": {},
"open_par": {},
"category": {
"0": "Doctrine"
}
}
}
]
}
While I need the following JSON to be output.
{
"total": 10,
"items": [
{
"item": {
"id": "234",
"title":"Indulged in any Good Vices Lately?",
"featured_image": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg",
"audio_file": "http://www.christianquestions.net/en/2002mp3/234_Indulge_in_any_good_vices_lately_12_15_02.mp3",
"youtube_id": "http://youtube.com/",
"open_par": "",
"category": "Social Issues"
}
},
{
"item": {
"id": "233",
"title":"Do You Use the Spirit of a Sound Mind?",
"featured_image": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg",
"audio_file": "http://www.christianquestions.net/en/2002mp3/233_Do_you_use_the_spirit_of_a_sound_mind_12_08_02.mp3",
"youtube_id": "http://youtube.com/",
"open_par": "",
"category": "Parable"
}
},
{
"item": {
"id": "232",
"title": "What Does it Mean to be Thankful?",
"featured_image": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg",
"audio_file": "http://www.christianquestions.net/en/2002mp3/232_What_does_it_mean_to_be_thankful_12_01_02.mp3",
"youtube_id": "http://youtube.com/v",
"open_par": "",
"category": "Inspiration"
}
}
]
}
Forget about the blank values I just need the JSON structure. Please suggest whats wrong in my code. Thanks
Edit. My arrayItem var_dump is following.
array(10) {
[0]=>
array(7) {
["id"]=>
string(3) "234"
["title"]=>
object(SimpleXMLElement)#3306 (1) {
[0]=>
string(34) "Indulged in any Good Vices Lately?"
}
["featured_image"]=>
object(SimpleXMLElement)#3319 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3314 (1) {
[0]=>
string(94) "http://www.christianquestions.net/en/2002mp3/234_Indulge_in_any_good_vices_lately_12_15_02.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3315 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3310 (0) {
}
["category"]=>
object(SimpleXMLElement)#3323 (1) {
[0]=>
string(13) "Social Issues"
}
}
[1]=>
array(7) {
["id"]=>
string(3) "233"
["title"]=>
object(SimpleXMLElement)#3312 (1) {
[0]=>
string(38) "Do You Use the Spirit of a Sound Mind?"
}
["featured_image"]=>
object(SimpleXMLElement)#3325 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3320 (1) {
[0]=>
string(99) "http://www.christianquestions.net/en/2002mp3/233_Do_you_use_the_spirit_of_a_sound_mind_12_08_02.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3321 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3316 (0) {
}
["category"]=>
object(SimpleXMLElement)#3329 (1) {
[0]=>
string(7) "Parable"
}
}
[2]=>
array(7) {
["id"]=>
string(3) "232"
["title"]=>
object(SimpleXMLElement)#3318 (1) {
[0]=>
string(33) "What Does it Mean to be Thankful?"
}
["featured_image"]=>
object(SimpleXMLElement)#3331 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3326 (1) {
[0]=>
string(94) "http://www.christianquestions.net/en/2002mp3/232_What_does_it_mean_to_be_thankful_12_01_02.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3327 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3322 (0) {
}
["category"]=>
object(SimpleXMLElement)#3335 (1) {
[0]=>
string(11) "Inspiration"
}
}
[3]=>
array(7) {
["id"]=>
string(3) "226"
["title"]=>
object(SimpleXMLElement)#3324 (1) {
[0]=>
string(13) "Who is Jesus?"
}
["featured_image"]=>
object(SimpleXMLElement)#3337 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3332 (1) {
[0]=>
string(33) "http://www.christianquestions.net"
}
["youtube_id"]=>
object(SimpleXMLElement)#3333 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3328 (0) {
}
["category"]=>
object(SimpleXMLElement)#3341 (1) {
[0]=>
string(8) "Doctrine"
}
}
[4]=>
array(7) {
["id"]=>
string(3) "216"
["title"]=>
object(SimpleXMLElement)#3330 (1) {
[0]=>
string(43) "Does God Really Torture People in Hellfire?"
}
["featured_image"]=>
object(SimpleXMLElement)#3343 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3338 (1) {
[0]=>
string(85) "http://www.christianquestions.net/en/2002mp3/216_Does_God_really_torture_Hellfire.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3339 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3334 (0) {
}
["category"]=>
object(SimpleXMLElement)#3347 (1) {
[0]=>
string(8) "Doctrine"
}
}
[5]=>
array(7) {
["id"]=>
string(3) "192"
["title"]=>
object(SimpleXMLElement)#3336 (1) {
[0]=>
string(34) "Why Don’t You Ever Listen to Me?"
}
["featured_image"]=>
object(SimpleXMLElement)#3349 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3344 (1) {
[0]=>
string(92) "http://www.christianquestions.net/en/2002mp3/192_Why_dont_you_ever_listen_to_me_02_10_02.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3345 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3340 (0) {
}
["category"]=>
object(SimpleXMLElement)#3353 (1) {
[0]=>
string(9) "Character"
}
}
[6]=>
array(7) {
["id"]=>
string(3) "188"
["title"]=>
object(SimpleXMLElement)#3342 (1) {
[0]=>
string(38) "Who Ends Up in the “Lake of Fire?”"
}
["featured_image"]=>
object(SimpleXMLElement)#3355 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3350 (1) {
[0]=>
string(84) "http://www.christianquestions.net/en/2002mp3/188_Who_ends_up_in_the_lake_of_fire.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3351 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3346 (0) {
}
["category"]=>
object(SimpleXMLElement)#3359 (1) {
[0]=>
string(8) "Doctrine"
}
}
[7]=>
array(7) {
["id"]=>
string(3) "156"
["title"]=>
object(SimpleXMLElement)#3348 (1) {
[0]=>
string(37) "Is Jesus Really God? (Parts III & IV)"
}
["featured_image"]=>
object(SimpleXMLElement)#3361 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3356 (1) {
[0]=>
string(72) "http://www.christianquestions.net/en/2001mp3/156_Is_Jesus_really_God.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3357 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3352 (0) {
}
["category"]=>
object(SimpleXMLElement)#3365 (1) {
[0]=>
string(8) "Doctrine"
}
}
[8]=>
array(7) {
["id"]=>
string(3) "155"
["title"]=>
object(SimpleXMLElement)#3354 (1) {
[0]=>
string(35) "Is Jesus Really God? (Parts I & II)"
}
["featured_image"]=>
object(SimpleXMLElement)#3367 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3362 (1) {
[0]=>
string(72) "http://www.christianquestions.net/en/2001mp3/155_Is_Jesus_really_God.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3363 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3358 (0) {
}
["category"]=>
object(SimpleXMLElement)#3371 (1) {
[0]=>
string(8) "Doctrine"
}
}
[9]=>
array(7) {
["id"]=>
string(3) "124"
["title"]=>
object(SimpleXMLElement)#3360 (1) {
[0]=>
string(42) "Did God Really Create the World in 6 Days?"
}
["featured_image"]=>
object(SimpleXMLElement)#3373 (1) {
[0]=>
string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
}
["audio_file"]=>
object(SimpleXMLElement)#3368 (1) {
[0]=>
string(96) "http://www.christianquestions.net/en/2000mp3/124_Did_God_create_the_world_in_6_days_10_22_00.mp3"
}
["youtube_id"]=>
object(SimpleXMLElement)#3369 (0) {
}
["open_par"]=>
object(SimpleXMLElement)#3364 (0) {
}
["category"]=>
object(SimpleXMLElement)#3377 (1) {
[0]=>
string(8) "Doctrine"
}
}
}
As seen in your var_dump, the title is not a string, but an object, causing this behavior. If you want json_encode to encode the value as a string, not as an object, make sure to give it a string, e.g. by casting the value:
'title' => (string)($arrayItem[$i]['title']),
Edit: Note that String conversion is not always that simple. Just casting works in this case because we're dealing with SimpleXMLElement objects. If you have regular objects/arrays, you may need to use something more appropriate (e.g. $myarray[0]).
I make a JSON call using an API and get the following, which seems to be correctly formatted JSON:
{
"pagination": {},
"meta": {
"code": 200
},
"data": [
{
"tags": [],
"location": {
"latitude": 37.42833,
"name": "Stanford University",
"longitude": -122.1668,
"id": 10138861
},
"comments": {
"count": 0,
"data": []
},
"filter": "Rise",
"created_time": "1331327429",
"link": "http://instagr.am/p/H91ykZpqUW/",
"likes": {
"count": 3,
"data": [
{
"username": "razzles39",
"profile_picture": "http://images.instagram.com/profiles/profile_14316422_75sq_1322705511.jpg",
"id": "14316422",
"full_name": "razzles39"
},
{
"username": "mscaliti",
"profile_picture": "http://images.instagram.com/profiles/profile_10827166_75sq_1330704753.jpg",
"id": "10827166",
"full_name": "mscaliti"
},
{
"username": "mariecox",
"profile_picture": "http://images.instagram.com/profiles/profile_3987147_75sq_1324863102.jpg",
"id": "3987147",
"full_name": "Marie Cox"
}
]
},
"images": {
"low_resolution": {
"url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_7.jpg",
"width": 612,
"height": 612
}
},
"caption": {
"created_time": "1331327500",
"text": "Chillin with Brady at Stanford",
"from": {
"username": "nicolelainefox",
"profile_picture": "http://images.instagram.com/profiles/anonymousUser.jpg",
"id": "17982472",
"full_name": "nicolelainefox"
},
"id": "143507936292283648"
},
"type": "image",
"id": "143507334669706518_17982472",
"user": {
"username": "nicolelainefox",
"website": "",
"bio": "",
"profile_picture": "http://images.instagram.com/profiles/anonymousUser.jpg",
"full_name": "nicolelainefox",
"id": "17982472"
}
}
]
}
I then use json_decode in my PHP script to make it something I can manipulate, and this is the output of var_dump
object(stdClass)#1 (3) { ["pagination"]=> object(stdClass)#2 (0) { } ["meta"]=> object(stdClass)#3 (1) { ["code"]=> int(200) } ["data"]=> array(1) { [0]=> object(stdClass)#4 (12) { ["tags"]=> array(0) { } ["location"]=> object(stdClass)#5 (4) { ["latitude"]=> float(37.42833) ["name"]=> string(19) "Stanford University" ["longitude"]=> float(-122.1668) ["id"]=> int(10138861) } ["comments"]=> object(stdClass)#6 (2) { ["count"]=> int(0) ["data"]=> array(0) { } } ["filter"]=> string(4) "Rise" ["created_time"]=> string(10) "1331327429" ["link"]=> string(31) "http://instagr.am/p/H91ykZpqUW/" ["likes"]=> object(stdClass)#7 (2) { ["count"]=> int(3) ["data"]=> array(3) { [0]=> object(stdClass)#8 (4) { ["username"]=> string(9) "razzles39" ["profile_picture"]=> string(73) "http://images.instagram.com/profiles/profile_14316422_75sq_1322705511.jpg" ["id"]=> string(8) "14316422" ["full_name"]=> string(9) "razzles39" } [1]=> object(stdClass)#9 (4) { ["username"]=> string(8) "mscaliti" ["profile_picture"]=> string(73) "http://images.instagram.com/profiles/profile_10827166_75sq_1330704753.jpg" ["id"]=> string(8) "10827166" ["full_name"]=> string(8) "mscaliti" } [2]=> object(stdClass)#10 (4) { ["username"]=> string(8) "mariecox" ["profile_picture"]=> string(72) "http://images.instagram.com/profiles/profile_3987147_75sq_1324863102.jpg" ["id"]=> string(7) "3987147" ["full_name"]=> string(9) "Marie Cox" } } } ["images"]=> object(stdClass)#11 (3) { ["low_resolution"]=> object(stdClass)#12 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_6.jpg" ["width"]=> int(306) ["height"]=> int(306) } ["thumbnail"]=> object(stdClass)#13 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_5.jpg" ["width"]=> int(150) ["height"]=> int(150) } ["standard_resolution"]=> object(stdClass)#14 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_7.jpg" ["width"]=> int(612) ["height"]=> int(612) } } ["caption"]=> object(stdClass)#15 (4) { ["created_time"]=> string(10) "1331327500" ["text"]=> string(30) "Chillin with Brady at Stanford" ["from"]=> object(stdClass)#16 (4) { ["username"]=> string(14) "nicolelainefox" ["profile_picture"]=> string(54) "http://images.instagram.com/profiles/anonymousUser.jpg" ["id"]=> string(8) "17982472" ["full_name"]=> string(14) "nicolelainefox" } ["id"]=> string(18) "143507936292283648" } ["type"]=> string(5) "image" ["id"]=> string(27) "143507334669706518_17982472" ["user"]=> object(stdClass)#17 (6) { ["username"]=> string(14) "nicolelainefox" ["website"]=> string(0) "" ["bio"]=> string(0) "" ["profile_picture"]=> string(54) "http://images.instagram.com/profiles/anonymousUser.jpg" ["full_name"]=> string(14) "nicolelainefox" ["id"]=> string(8) "17982472" } } } }
However, when I use echo($instagram_data["data"]); everything crashes. How do I access the 'data' array in this associative array? Here's the whole code:
$instagram_handler = fopen("https://api.instagram.com/v1/locations/10138861/media/recent/?client_id=MY_ID", "r");
$instagram_json = stream_get_contents($instagram_handler);
fclose($instagram_handler);
$instagram_data = json_decode($instagram_json);
echo($instagram_data["data"]); //Breaks page
the JSON is read as an object, not an associative array. load it like so and you'll be good:
$instagram_data = json_decode($instagram_json, TRUE);
alternatively, loading it like you do currently, run:
echo $instagram_data->data
Note the assoc param of json_decode function. If you omit it decode treats JSON as an object rather than an array. So you should be able access it with:
$instagram_data->data;
Other option is to decode as array as following:
$instagram_data = json_decode($instagram_json, TRUE);