galvin Posted January 22, 2011 Share Posted January 22, 2011 I have burned through about 6 hours of time trying to figure this out for myself, but just cannot get it to work at all. It's incredibly frustrating because I know enough programming to know that it shouldn't be this hard to figure out . I have an PHP array called $_SESSION['list']. It consists of... Array ([1] => Miami [2] => Magic [4] => Thunder [5] => Lightning [6] => Avalanche [8] => Red Sox [9] => White Sox ) I need to receive the information in this array on my main page (called main.php) via a basic AJAX call to a page called "getlist.php" and have it put into a JAVASCRIPT array. In other words, I call to getlist.php, a page that stores the array called $_SESSION['list']. I need the proper steps to happen where the ultimate result is that I have a JAVASCRIPT array back on main.php that stores the exact same items and index numbers that the PHP array called $_SESSION['list'] stored, so that I can then do other stuff on main.php using this new JAVASCRIPT array. From my hours of research, it sounds like this might have to be done using JSON (which I had never heard of until now) and echoing the JSON data from getlist.php and then manipulating that info to create a Javascript array back on main.php. Even if that is correct, I just can't figure out HOW to do that. Frankly, I don't care how it gets done at this point, I just need to get it done. If ANYONE can take a few minutes to show me HOW, please do. Please, please, please, I am desperate at this point. Thanks, Greg Quote Link to comment https://forums.phpfreaks.com/topic/225338-turn-php-array-into-javascript-array-via-ajaxwith-the-exact-same-itemsindexes/ Share on other sites More sharing options...
ignace Posted January 22, 2011 Share Posted January 22, 2011 json_encode echo json_encode(array(1 => 'Miami', 2 => 'Magic' 4 => 'Thunder' 5 => 'Lightning' 6 => 'Avalanche' 8 => 'Red Sox' 9 => 'White Sox' )); Quote Link to comment https://forums.phpfreaks.com/topic/225338-turn-php-array-into-javascript-array-via-ajaxwith-the-exact-same-itemsindexes/#findComment-1163682 Share on other sites More sharing options...
galvin Posted January 22, 2011 Author Share Posted January 22, 2011 Ok thanks, I am finally getting some actually JSON data back on my main page via AJAX. This is what it looks like... {"2":"Magic ","4":"Thunder","5":"Lightning ","6":"Avalanche","8":"Red Sox","9":"White Sox "} Now, how do I most easily/efficentially turn that into a Javascript Array? Basically, I want a Javascript array (we can call it myList) that has this info... myList[2]="Magic"; myList[4]="Thunder"; myList[5]="Lightning"; myList[6]="Avalanche"; myList[8]="Red Sox"; myList[9]="White Sox"; How do I turn that JSON stuff into that myList array above? Quote Link to comment https://forums.phpfreaks.com/topic/225338-turn-php-array-into-javascript-array-via-ajaxwith-the-exact-same-itemsindexes/#findComment-1163688 Share on other sites More sharing options...
ManiacDan Posted January 22, 2011 Share Posted January 22, 2011 Google is your friend Quote Link to comment https://forums.phpfreaks.com/topic/225338-turn-php-array-into-javascript-array-via-ajaxwith-the-exact-same-itemsindexes/#findComment-1163705 Share on other sites More sharing options...
galvin Posted January 22, 2011 Author Share Posted January 22, 2011 I am using eval. Probably not correctly, but I'm trying . Problem is that when I loop through that array (not positive it's technically an array yet, hence my confusion), instead of getting individual items in the array (Miami Heat, Orlando Magic, etc, etc), it's giving me literally every CHARACTER from the array (",1,",:,",M,i,a,m,i, etc)). For example,say the JSON object (called "jsondata") has this data... {"1":"Miami Heat","2":"Orlando Magic ","3":"Oklahoma City Thunder","4":"Tampa Bay Lightning ","5":"Colorado Avalanche","6":"Minnesota Wild","7":"Boston Red Sox","8":"Chicago White Sox ", "9":"New York Mets ", "10":"Philadelphia Phillies "} And I do.. myList = eval(jsondata); And then I loop through using this code (via AJAX)... for(var i = 1; i <= 10; i++) { document.getElementById('answer'+i).innerHTML = myList[i]; } My HTML does update, but I get a table whose HTML equates to... <td id="answer1">"</td> <td id="answer2">1</td> <td id="answer3">"</td> <td id="answer4">:</td> <td id="answer5">"</td> <td id="answer6">M</td> <td id="answer7">i</td> <td id="answer8">a</td> <td id="answer9">m</td> <td id="answer10">i</td> But what I want is this (where the answer id matches the "index" number in the JSON object)... <td id="answer1">Miami Heat</td> <td id="answer2">Orlando Magic</td> <td id="answer3">Oklahoma City Thunder</td> <td id="answer4">Tampa Bay Lightning</td> <td id="answer5">Colorado Avalanche</td> <td id="answer6">Minnesota Wild</td> <td id="answer7">Boston Red Sox</td> <td id="answer8">Chicago White Sox</td> <td id="answer9">New York Mets</td> <td id="answer10">Philadelphia Phillies</td> My disconnect is clearly with the JSON data and how it relates to an actual array (i.e. if it has to be turned into an array or already is technically an array). In it's simplest form, all I want to do is know how to turn JSON data like this... {"1":"Miami Heat","2":"Orlando Magic ","3":"Oklahoma City Thunder","4":"Tampa Bay Lightning ","5":"Colorado Avalanche","6":"Minnesota Wild","7":"Boston Red Sox","8":"Chicago White Sox ", "9":"New York Mets ", "10":"Philadelphia Phillies "} Into a Javascript Array that looks like this... myList[1]="Miami Heat"; myList[2]="Orlando Magic"; myList[3]="Oklahoma City Thunder"; myList[4]="Tampa Bay Lightning"; myList[5]="Colorado Avalanche"; myList[6]="Minnesota Wild"; myList[7]="Boston Red Sox"; myList[8]="Chicago White Sox"; myList[9]="New York Mets"; myList[10]="Philadelphia Phillies"; If I could do that, then I know I could get my code to do what I want. I know I'm doing something(s) stupid/newbie-ish, and/or just not understanding a core principle, so please help me understand if you can make sense of my issue. Quote Link to comment https://forums.phpfreaks.com/topic/225338-turn-php-array-into-javascript-array-via-ajaxwith-the-exact-same-itemsindexes/#findComment-1163715 Share on other sites More sharing options...
ignace Posted January 22, 2011 Share Posted January 22, 2011 myList = eval(jsondata); Should be: myList = eval('(' + jsondata + ')'); Quote Link to comment https://forums.phpfreaks.com/topic/225338-turn-php-array-into-javascript-array-via-ajaxwith-the-exact-same-itemsindexes/#findComment-1163730 Share on other sites More sharing options...
galvin Posted January 22, 2011 Author Share Posted January 22, 2011 Thank you for correcting my mistake, ignace... That change seems to have done it! Quote Link to comment https://forums.phpfreaks.com/topic/225338-turn-php-array-into-javascript-array-via-ajaxwith-the-exact-same-itemsindexes/#findComment-1163744 Share on other sites More sharing options...
ignace Posted January 23, 2011 Share Posted January 23, 2011 If you are throwing this through a function you may aswell just use JSONP: <script type="text/javascript" src="script.php?callback=jsFunction&arg1=value1&arg2=.."></script> jsFunction({"1":"Miami Heat","2":"Orlando Magic ","3":"Oklahoma City Thunder","4":"Tampa Bay Lightning ","5":"Colorado Avalanche","6":"Minnesota Wild","7":"Boston Red Sox","8":"Chicago White Sox ", "9":"New York Mets ", "10":"Philadelphia Phillies "}); JSONP means "JSON with Padding" where padding is the callback function, all you need to do is prepend the callback (jsFunction) and upon calling the script (src="..") it will be executed like regular JS, your PHP would look like: echo $callback, '(', json_encode($array), ');'; // jsFunction(..); Quote Link to comment https://forums.phpfreaks.com/topic/225338-turn-php-array-into-javascript-array-via-ajaxwith-the-exact-same-itemsindexes/#findComment-1163887 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.