Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Me1000

macrumors 68000
Original poster
Jul 15, 2006
1,794
4
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20)

I have a multi dim array in Javascript
Code:
array[0][0]
I need to pass the array to PHP (using an Ajax call probably)
I have the jquery framework loaded up to work with.

What would be the best way to pass the array to PHP?

Thanks
 
Can you elaborate on the length and type of information in the array for both dimensions? It might give some of us some ideas about a best approach.

Addition: Some small snippets. Example uses a 2x3 array.
PHP:
// JavaScript

function SendArray()
{
  var arr = [[0,1], [2,3], [4,5]]; // 2x3 array
  var a = escape(arr.toString());
  ajaxCall('phpPage.php?array='+a); // not a real function
}

// PHP

// Grab passed value, decode the query passed in, then explode it based on the comma
$passedArray = explode(',', rawurldecode($_GET['array']));
// result: 0,1,2,3,4,5
$newArray = array();
// process the string
for ($a=0, $b=0, $c=count($passedArray); $b<$c;) {
  $newArray[$a][0] = $passedArray[$b++];
  $newArray[$a++][1] = $passedArray[$b++];
}
 
Both dims contain a small text string.
The first is a filename.
The second is a caption for the file which is named in the first dim.

I'll look at your code further tomorrow, thank you for the help!
:apple:
 
Both dims contain a small text string.
The first is a filename.
The second is a caption for the file which is named in the first dim.

I'll look at your code further tomorrow, thank you for the help!
:apple:

As long as those strings don't contain any commas, I think my code should give a good head start. I didn't provide everything you need, but if you need further guidance or clarification just let me know.

Also, if anyone else has ideas please feel free to share. I just put out the first idea that came to me.
 
I could see people using commas in the caption... :eek:
 
I could see people using commas in the caption... :eek:

Hmm, good call. Then an alternative is to separate the array values using something other than the comma. So a new function would need to be created to use rather than the toString() function that conjoins the array elements. Maybe use "|" or some uncommonly used character or you could use a couple characters, e.g., "|@|". Then in the PHP when you explode the string you'll enter what character(s) you used as the separator.

Is that something you can do or do you need guidance?
 
ALright this is what I came up with...
Code:
function toStringComplex(array)
{
	var string;
	
	for(var i = 0; i < array.length; i++)
	{
		string = string + "|@|" + array[i];
	}
	return string;
}
var a = escape(toStringComplex(itemsAll));

I pass the a string to the php file

Code:
 window.location = "parseData.php?data="+a;
That might not be my final code to send it to the php file, but right now that is the easiest to work with.

problem is,
my url looks something like this:
Code:
parseData.php?data=undefined|@|IMG_0608%2CIMG_0608|@|blahblah|@|blahblah...

That "undefined" part in the beginning is causing a problem when im parsing through the data.

Any idea why that is happening?

when i call
Code:
alert(itemsAll[0][0]);
it displays the correct result, so I know my original array is good
 
Yup, in your for loop you have string = string + ..., but string isn't declared the first time through. Just before the for loop when you declare the string variable set it equal to "" so it has a starting value.

Also, you'll likely need a double for loop since you're wanting to work with multi-dimensional array.
 
yay!

Thank you for your help.


The final javascript code I ended up with, for those of you that are interested:

Code:
function toStringComplex(array)
{
	var string;
	
	for(var i = 0; i < array.length; i++)
	{
		if(string == null){
			string = array[i][0] + "|@|" + array[i][1];
		}else{
			string = string + "|@|" + array[i][0] + "|@|" + array[i][1];
		}
	}
	return string;
}
 
Seems like you got it figured out, but you might also want to do some research into JSON.

JSON (Javascript Object Notation) allows you to encode javascript objects in a compact notation that can be sent over the wire. Most other languages have libraries to parse this JSON into native objects. I'm sure if you research it you'll get a better idea of what I mean.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.