Jump to content

Turn PHP array into Javascript array (via Ajax)with the exact same items/indexes


galvin

Recommended Posts

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

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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(..);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.