1

I am sending a CSV list to the server within the url. It is a list of songid's:

var $array = [];


        oTable.find('td').each(function(){

            $songdata = $(this).data('data');
            $songid = $songdata.songid;

    //The next line is the variable I need to also send along with each songid
            $duration = $songdata.duration;


            $array.push($songid)



            });

This results in '24,25,32,48,87' being sent to the server.

I then use the following in my php script to convert it into an array:

 $songs = $_GET['songid'];

 $songarray = explode(",", $songs);

This works great, I can access the data properly and all is well.

However I now have the need to add another property to each of the songid's. As seen above, I need to add the $duration variable to the data sent and be able to convert it into an array server side. (or alternatively construct the actual array client side would be fine also)

How would I go about this?

Pratik
  • 11,534
  • 22
  • 69
  • 99
gordyr
  • 6,078
  • 14
  • 65
  • 123

5 Answers5

2

I would create an object of the data in your jscript and send it over to the PHP and then use json_decode then you have an associative array of your data within the PHP

Ashley Banks
  • 528
  • 5
  • 16
2

You could use this to encode your array into JSON:
http://www.openjs.com/scripts/data/json_encode.php

And then send that String to your backend where you can unpack it with this:

$yourarray = json_decode($string)
bardiir
  • 14,556
  • 9
  • 41
  • 66
  • Thanks to all for the answers, There are many correct ones and methods I could use that have been suggested however, this was the one I ended up going with and I can see this function being very useful elsewhere in my application. Thanks to all. – gordyr Jan 10 '12 at 12:19
1

You always can use JSON for data serialization/deserealization as of Serializing to JSON in jQuery

Community
  • 1
  • 1
alrusdi
  • 1,273
  • 1
  • 7
  • 9
0

try sending arrays to PHP in JSON format and then do json_decode($var) in your PHP script.

Roman
  • 3,799
  • 4
  • 30
  • 41
0

make an associative array server side

$array = new Array();

//add songid as key and duration as value
$array = array_push_assoc($array, $songid, $songduration);


echo json_encode($array);

on the client side after parsing the json

$.each(json,function(key,value){
//key will be the songid
//value will be the duration
});
Rafay
  • 30,950
  • 5
  • 68
  • 101