0

配列オブジェクトからの値へのアクセスに問題があります。

PHPから、このようなオブジェクトの配列を送信します

initiate_data(".json_encode($my_array).");

JavaScriptから配列をチェックするには、このコードがあります

function initiate_data(my_array){
window.alert(my_array);}

私が持っているデータは

{'name':'name1','surname':'surname1','email':'email1'},{'name':'name2','surname':'surname2','email':'email2'},{'name':'name3','surname':'surname3','email':'email3'}

最初のオブジェクトにもアクセスできます

window.alert(my_array[0]);

結果は

{'name':'name1','surname':'surname1','email':'email1'}

私の問題は、オブジェクトから名前にアクセスできないことです。

私は試してみました(my_array[0].name)が、未定義になります

解決策をグーグルで検索しましたが、私の問題に固有の解決策を得ることができません

my_array[0][0] にアクセスしようとすると、結果 {

これまでお世話になりました

console.log は私にこれを与えます

["{"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"}"]

追加情報**

php クラスは JsonSerializable を実装し、オブジェクトの配列を作成しました。これが問題の明確化に役立つことを願っています

一時的な修正

彼らはそれを行うためのより良い方法かもしれませんが、これは私が問題を解決するために行ったことです

var my_obj = JSON.parse(my_array[0]);

名前にアクセスできるようになりました

console.log(my_obj.name);

名前を教えて1

一時的な修正、すべての配列を変数に渡すことができるまで。

4

2 に答える 2

1

$my_arrayjson文字列が含まれているようです:

$my_array = array(
    '{"name":"name1","surname":"surname1","email":"email1"}',
    '{"name":"name2","surname":"surname2","email":"email2"}',
    '{"name":"name3","surname":"surname3","email":"email3"}'
);

その場合は、単純な破裂によってその javascript 配列を作成し、ブラケットを追加できます。

initiate_data([".implode(',',$my_array)."]);
于 2013-07-21T11:35:25.903 に答える