Passing JS multidimensional array to PHP

Discussion in 'Web Design and Development' started by Me1000, Feb 3, 2009.

  1. Me1000 macrumors 68000

    Me1000

    Joined:
    Jul 15, 2006
    #1
    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
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    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 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++];
    }
     
  3. Me1000 thread starter macrumors 68000

    Me1000

    Joined:
    Jul 15, 2006
    #3
    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:
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    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.
     
  5. Me1000 thread starter macrumors 68000

    Me1000

    Joined:
    Jul 15, 2006
    #5
    I could see people using commas in the caption... :eek:
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    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?
     
  7. Me1000 thread starter macrumors 68000

    Me1000

    Joined:
    Jul 15, 2006
    #7
    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
     
  8. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #8
    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.
     
  9. Me1000 thread starter macrumors 68000

    Me1000

    Joined:
    Jul 15, 2006
    #9
    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;
    }
     
  10. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #10
    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.
     

Share This Page