Access JSON_encode array objects - php
I have a problem access values from array objects.
From PHP I send my array of objects like this
initiate_data(".json_encode($my_array).");
From javascript to check the array I have this code
function initiate_data(my_array){
window.alert(my_array);}
The data I have is
{'name':'name1','surname':'surname1','email':'email1'},{'name':'name2','surname':'surname2','email':'email2'},{'name':'name3','surname':'surname3','email':'email3'}
I am also able to access the first object
window.alert(my_array[0]);
The result is
{'name':'name1','surname':'surname1','email':'email1'}
My problem is that I cannot access name from the object.
I have tried (my_array[0].name) and I get undefined
I googled for the solution but I cannot get a solution specific to my problem
when I try to access my_array[0][0] the result {
Thank you for your help thus far
console.log gives me this
["{"rec":"1","name":"Home","code":"mmenu_home","type":"1","level":"0","default_view":"show_post"}", "{"rec":"2","name":"Home","code":"mmenu_home","type":"2","level":"0","default_view":"show_post"}", "{"rec":"3","name":"Home","code":"mmenu_home","type":"3","level":"1","default_view":"show_post"}", "{"rec":"4","name":"Home","code":"mmenu_home","type":"3","level":"2","default_view":"show_post"}", "{"rec":"5","name":"Home","code":"mmenu_home","type":"3","level":"3","default_view":"show_post"}", "{"rec":"6","name":"Home","code":"mmenu_home","type":"3","level":"4","default_view":"show_post"}", "{"rec":"7","name":"Home","code":"mmenu_home","type":"3","level":"5","default_view":"show_post"}", "{"rec":"8","name":"Home","code":"mmenu_home","type":"3","level":"6","default_view":"show_post"}", "{"rec":"9","name":"Home","code":"mmenu_home","type":"4","level":"0","default_view":"show_post"}", "{"rec":"10","name":"Opporunities","code":"mmenu_op…":"1","level":"0","default_view":"show_opp_tips"}", "{"rec":"11","name":"Careers","code":"mmenu_careers…e":"1","level":"0","default_view":"show_careers"}", "{"rec":"12","name":"Tertiary","code":"mmenu_tertia…":"1","level":"0","default_view":"show_tertiary"}", "{"rec":"13","name":"News","code":"mmenu_news","type":"1","level":"0","default_view":"show_news"}", "{"rec":"14","name":"News","code":"mmenu_news","type":"2","level":"0","default_view":"show_news"}", "{"rec":"15","name":"News","code":"mmenu_news","type":"3","level":"1","default_view":"show_news"}", "{"rec":"16","name":"News","code":"mmenu_news","type":"3","level":"2","default_view":"show_news"}", "{"rec":"17","name":"News","code":"mmenu_news","type":"3","level":"3","default_view":"show_news"}", "{"rec":"18","name":"News","code":"mmenu_news","type":"3","level":"4","default_view":"show_news"}", "{"rec":"19","name":"News","code":"mmenu_news","type":"3","level":"5","default_view":"show_news"}", "{"rec":"20","name":"News","code":"mmenu_news","type":"3","level":"6","default_view":"show_news"}", "{"rec":"21","name":"News","code":"mmenu_news","type":"4","level":"0","default_view":"show_news"}", "{"rec":"22","name":"Profile","code":"mmenu_profile…e":"1","level":"0","default_view":"show_profile"}", "{"rec":"23","name":"Profile","code":"mmenu_profile…e":"2","level":"0","default_view":"show_profile"}", "{"rec":"24","name":"Profile","code":"mmenu_profile…e":"3","level":"1","default_view":"show_profile"}", "{"rec":"25","name":"Profile","code":"mmenu_profile…e":"3","level":"2","default_view":"show_profile"}", "{"rec":"26","name":"Profile","code":"mmenu_profile…e":"3","level":"3","default_view":"show_profile"}", "{"rec":"27","name":"Profile","code":"mmenu_profile…e":"3","level":"4","default_view":"show_profile"}", "{"rec":"28","name":"Profile","code":"mmenu_profile…e":"3","level":"5","default_view":"show_profile"}", "{"rec":"29","name":"Profile","code":"mmenu_profile…e":"3","level":"6","default_view":"show_profile"}", "{"rec":"30","name":"Profile","code":"mmenu_profile…e":"4","level":"0","default_view":"show_profile"}", "{"rec":"31","name":"SMS","code":"sms_send_function…"type":"3","level":"2","default_view":"sms_home"}", "{"rec":"32","name":"SMS","code":"sms_send_function…"type":"3","level":"5","default_view":"sms_home"}", "{"rec":"33","name":"SMS","code":"sms_send_function…"type":"3","level":"6","default_view":"sms_home"}", "{"rec":"34","name":"Learners","code":"learners_mme…":"3","level":"2","default_view":"view_learners"}", "{"rec":"35","name":"Learners","code":"learners_mme…":"3","level":"3","default_view":"view_learners"}", "{"rec":"36","name":"Learners","code":"learners_mme…":"3","level":"4","default_view":"view_learners"}", "{"rec":"37","name":"Learners","code":"learners_mme…":"3","level":"5","default_view":"view_learners"}", "{"rec":"38","name":"Learners","code":"learners_mme…":"3","level":"6","default_view":"view_learners"}", "{"rec":"39","name":"Parents","code":"parents_mmenu…e":"3","level":"2","default_view":"view_parents"}", "{"rec":"40","name":"Parents","code":"parents_mmenu…e":"3","level":"3","default_view":"view_parents"}", "{"rec":"41","name":"Parents","code":"parents_mmenu…e":"3","level":"4","default_view":"view_parents"}", "{"rec":"42","name":"Parents","code":"parents_mmenu…e":"3","level":"5","default_view":"view_parents"}", "{"rec":"43","name":"Parents","code":"parents_mmenu…e":"3","level":"6","default_view":"view_parents"}", "{"rec":"44","name":"Staff","code":"staff_function"…:"3","level":"2","default_view":"staff_view_all"}", "{"rec":"45","name":"Staff","code":"staff_function"…:"3","level":"5","default_view":"staff_view_all"}", "{"rec":"46","name":"Staff","code":"staff_function"…:"3","level":"6","default_view":"staff_view_all"}", "{"rec":"47","name":"SGB","code":"sgb_home","type":"3","level":"2","default_view":"sgb_view_all"}", "{"rec":"48","name":"SGB","code":"sgb_home","type":"3","level":"5","default_view":"sgb_view_all"}", "{"rec":"49","name":"SGB","code":"sgb_home","type":"3","level":"6","default_view":"sgb_view_all"}", "{"rec":"50","name":"Photos","code":"photos_mmenu",…vel":"2","default_view":"view_all_websitephotos"}", "{"rec":"51","name":"Photos","code":"photos_mmenu",…vel":"5","default_view":"view_all_websitephotos"}", "{"rec":"52","name":"Photos","code":"photos_mmenu",…vel":"6","default_view":"view_all_websitephotos"}", "{"rec":"53","name":"Departments","code":"mm_depart…3","level":"2","default_view":"view_departments"}", "{"rec":"54","name":"Departments","code":"mm_depart…3","level":"5","default_view":"view_departments"}", "{"rec":"55","name":"Departments","code":"mm_depart…3","level":"6","default_view":"view_departments"}", "{"rec":"56","name":"Academic","code":"mm_academic"…":"3","level":"2","default_view":"view_academic"}", "{"rec":"57","name":"Academic","code":"mm_academic"…":"3","level":"5","default_view":"view_academic"}", "{"rec":"58","name":"Academic","code":"mm_academic"…":"3","level":"6","default_view":"view_academic"}", "{"rec":"59","name":"Term","code":"mm_term","type":"1","level":"0","default_view":"view_term"}", "{"rec":"60","name":"Term","code":"mm_term","type":"2","level":"0","default_view":"view_term"}", "{"rec":"61","name":"Term","code":"mm_term","type":"3","level":"1","default_view":"view_term"}", "{"rec":"62","name":"Term","code":"mm_term","type":"3","level":"2","default_view":"view_term"}", "{"rec":"63","name":"Term","code":"mm_term","type":"3","level":"3","default_view":"view_term"}", "{"rec":"64","name":"Term","code":"mm_term","type":"3","level":"4","default_view":"view_term"}", "{"rec":"65","name":"Term","code":"mm_term","type":"3","level":"5","default_view":"view_term"}", "{"rec":"66","name":"Term","code":"mm_term","type":"3","level":"6","default_view":"view_term"}", "{"rec":"67","name":"Sports","code":"mm_sports","type":"1","level":"0","default_view":"view_sports"}", "{"rec":"68","name":"Sports","code":"mm_sports","type":"2","level":"0","default_view":"view_sports"}", "{"rec":"69","name":"Sports","code":"mm_sports","type":"3","level":"1","default_view":"view_sports"}", "{"rec":"70","name":"Sports","code":"mm_sports","type":"3","level":"2","default_view":"view_sports"}", "{"rec":"71","name":"Sports","code":"mm_sports","type":"3","level":"3","default_view":"view_sports"}", "{"rec":"72","name":"Sports","code":"mm_sports","type":"3","level":"4","default_view":"view_sports"}", "{"rec":"73","name":"Sports","code":"mm_sports","type":"3","level":"5","default_view":"view_sports"}", "{"rec":"74","name":"Sports","code":"mm_sports","type":"3","level":"6","default_view":"view_sports"}", "{"rec":"75","name":"Timetable","code":"mm_timetabl…:"1","level":"0","default_view":"view_belltimes"}", "{"rec":"76","name":"Timetable","code":"mm_timetabl…:"2","level":"0","default_view":"view_belltimes"}", "{"rec":"77","name":"Timetable","code":"mm_timetabl…:"3","level":"1","default_view":"view_belltimes"}", "{"rec":"78","name":"Timetable","code":"mm_timetabl…:"3","level":"2","default_view":"view_belltimes"}", "{"rec":"79","name":"Timetable","code":"mm_timetabl…:"3","level":"3","default_view":"view_belltimes"}", "{"rec":"80","name":"Timetable","code":"mm_timetabl…:"3","level":"4","default_view":"view_belltimes"}", "{"rec":"81","name":"Timetable","code":"mm_timetabl…:"3","level":"5","default_view":"view_belltimes"}", "{"rec":"82","name":"Timetable","code":"mm_timetabl…:"3","level":"6","default_view":"view_belltimes"}"]
Additional Info**
The php class implements JsonSerializable and I have created a array of the object, I hope this will help in clarifying my problem
Temporary Fix
Although they might be a better way to do it, this is what I have done to solve the problem
var my_obj = JSON.parse(my_array[0]);
Now I am able to access name
console.log(my_obj.name);
Gives me name1
Temporary Fix, Until I am able to pass all the array to a variable.
Looks like your $my_array contains json strings:
$my_array = array(
'{"name":"name1","surname":"surname1","email":"email1"}',
'{"name":"name2","surname":"surname2","email":"email2"}',
'{"name":"name3","surname":"surname3","email":"email3"}'
);
If it is the case, you can create javascript array of it by simple implode and add brackets:
initiate_data([".implode(',',$my_array)."]);
is the json well encoded??
http://jsonlint.com/
{
"obj1": {
"name": "name1",
"surname": "surname1",
"email": "email1"
},
"obj3": {
"name": "name1",
"surname": "surname1",
"email": "email1"
},
"obj2": {
"name": "name1",
"surname": "surname1",
"email": "email1"
}
}
Related
How to access Multi Dimension Arrays/Arrays of Arrays [duplicate]
This question already has an answer here: How to extract and access data from JSON with PHP? (1 answer) Closed 8 months ago. I'm just trying to get into PHP and understand the core concepts, so have been messing around with arrays, jsons, etc. For this, I've tried everything, yet I cannot get to access the "Name" component of my Json. I recieve it from a CURL HTTP Request, decode it from a json then attempt to go from there. The JSON looks like: [ { "food1": "blah", "food2": "blah", "food3": "blah" }, { "food1": "blah", "data": [ { "name": "I need to access this", "name2": "Another name" } ] } ] I've tried $Result[0]["data"]["name"], $result[0][1]["data"]["name"] and plenty more combinations of that kind of nature, yet still haven't been able to figure it out. Any help would be greatly appreciated, thanks. :)
In your specific example you'd use $array[1]['data'][0]['name'] A breakdown of your array: [ // this is the 0th element { "food1": "blah", "food2": "blah", "food3": "blah" }, // this is the 1st element { "food1": "blah", // 1st element's data property "data": [ // 1st element's data property's 0th element { // [1]st element's [data] property's [0]th element's [name] property "name": "I need to access this", "name2": "Another name" } ] } ]
Targeting object within array JSON Steam API
How do I access webm->max in this Steam API? It's the order [{ that confuses me, array of one before object? I'm not quite sure about the targeting here.. I've tried: $gameTrailer = $game_json->57690->data->movies[0]->webm->max; and $gameTrailer = $game_json['57690']['data']['movies']['webm']['max']; The API text is like this: "movies": [{ "id": 2029441, "name": "Tropico 4 Gameplay Trailer", "thumbnail": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2029441\/movie.293x165.jpg?t=1447358847", "webm": { "480": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2029441\/movie480.webm?t=1447358847", "max": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2029441\/movie_max.webm?t=1447358847" }, "highlight": true }], and "movies" lies within: {"57690": { "data": { Assume I'll always want the very first movie in an array (which in this case is an array of one). Thanks in advance.
Correct syntax: $game_json->{57690}->data->movies[0]->webm->max When you have an object with a numeric key, you have to wrap the key name by curly brackets (Numeric keys are not valid property names). If you use the associative option: json_decode( $data, True ); your second attempt is almost right. Simply add the correct index after movie: $gameTrailer = $game_json['57690']['data']['movies'][0]['webm']['max'];
Unable to fetch values from json array tree in codeigniter
I have following array tree in json which is passed to view in a variable [{ "root":"0", "id":"19", "name":"Rose", "childs":[{ "root":"19" "id":"22", "name":"Ceema", "childs":[{ "root":"22", "id":"49", "name":"Chandar" }, { "root":"22", "id":"50", "name":"Carol" }] }, { "root":"19", "id":"23", "name":"Ben" }] }] now i want that at first it displays the name of array with root 0, when we click on that name, it shows the name contained in arrays within child(if it has any). M not so good with json, so please help!
Use json_decode() function in php or if you want to do it in javascript JSON.parse This will give you result in array and rest things you need to do or find the code for it.
Retrieve objects inside json array
I'm trying to retrieve team1 score however i cant seem to figure out how to output this. So far i've got this to work where $obj is the json output. $obj->recent JSON "recent": [ [ { "match_id": "64886", "has_vods": false, "game": "dota2", "team 1": { "score": "", "name": "Wheel Whreck While Whistling", "bet": "7%" }, "team 2": { "score": "", "name": "Evil Geniuses DotA2", "bet": "68%" }, "live in": "1m 42s", "title": "Wheel Whreck... 7% vs 68% Evil...", "url": "", "tounament": "", "simple_title": "Wheel Whreck... vs Evil...", "streams": [] } ]
You need to use json_decode(); This function returns proper object with arrays and objects inside. Now you need check what is an object and what is an array. $obj = json_decode($obj, true); $obj->recent; //array $obj->recent[0]; //first element of array $obj->recent[0][0]; //first element of second array $obj->recent[0][0]->{'team 1'}; //access to object team 1 $obj->recent[0][0]->{'team 1'}->score; //access to property of object team 1 You can find this helpful to understand what happens; You can also check example on json_decode documentation If you use var_dump function on $obj it will show you what is an array and what is an object.
You'll want to use json_decode to get that into an array. It looks like recent is an array of arrays of objects. So, you'll do something like $json = json_decode($obj->recent, true); $team1 = $json[0][0]['team 1']; //should return array $score = $team1['score'] edit: Thanks for the comment, was missing a true as the second param in json_decode
PHP/MongoDB: update a value in an array
I have the following mongodb object: { "_id": ObjectId("4d0b9c7a8b012fe287547157"), "messages": { "0": { "toUname": "Eamorr3", "fromUname": "Eamorr2", "time": 1292606586, "id": "ABCDZZZ", "subject": "asdf", "message": "asdf", "read": 0 //I want to change this to 1! }, "1": { "toUname": "Eamorr1", "fromUname": "Eamorr3", "time": 1292606586, "id": "EFGHZZZ", "subject": "asdf2", "message": "asdf2", "read": 0 } }, "uname": "Eamorr3" } How do I set "read" to 1 where id=ABCDZZZZ? I'm using PHP. I've tried the following command: $driverInboxes->update(array('uname'=>$uname),array('$set'=>array('messages'=>array('id'=>$id,'read'=>'1')))); But when I do this, overwriting occurs and I get: { "_id": ObjectId("4d0b9c7a8b012fe287547157"), "messages": { "id": "j7zwr2hzx14d3sucmvp5", "read": "1" }, "uname": "Eamorr3" } I'm totally stuck. Any help much appreciated. Do I need to pull the entire array element, modify and and push it back in again? Many thanks in advance,
If you read your command, you're actually saying: "UPDATE WHERE uname = Eamorr3 SET messages equal to this array (id=blah,read=1)" When you do a $set on messages, you're basically instructing it to take your array as the new value. However, it looks like you're trying to update a specific message as read which is just a little more complex. So there are two hurdles here: 1: You're actually updating messages.0.read If you do array('$set' => array( 'messages.0.read' => 1 ) ), you will update the correct element. Follow that chain, messages is a javascript object and you want to update the property 0. The property 0 is itself a javascript object which contains the property read which you want to update. Can you see how you're updating messages.0.read? This brings us to problem #2. 2: the 0 is a problem for you If you look at the way you've structured the data in Mongo, the messages object is really sub-par. The "0" and "1" are currently acting as "ids" and they're not very useful. Personally, I would structure your objects with the actual IDs in place of "0" or "1". So your objects would look like the following: { "_id": ObjectId("4d0b9c7a8b012fe287547157"), "messages": { "ABCDZZZ": { "toUname": "Eamorr3", "fromUname": "Eamorr2", "time": 1292606586, "subject": "asdf", "message": "asdf", "read": 0 //I want to change this to 1! } }, "uname": "Eamorr3" } Now you're update command becomes this: array('$set' => array( 'messages.ABCDZZZ.read' => 1 ) ) This structure makes it much easier to update a specific message or a specific portion of a message.
If you want to keep the array structure for various purposes, you can use the Positional operator. This enables you to take advantage of array features ($pop,$push,etc) while simultaneously being able to update elements which are in an unknown array position.