1

How do i take a php array

$arr = (('date'=>'03-22-2012', 'count'=>1000 ), ('date'=>'03-23-2011', 'count'=>1170 ));

and convert it to:

var arr = [['03-22-2012', 1000], ['03-23-2011', 1170]]

usable by a javascript function?

is there an easy way to do this?

algorithmicCoder
  • 6,595
  • 20
  • 68
  • 117
  • possible duplicate of [Assigning a php array into a javascript array](http://stackoverflow.com/questions/8167620/assigning-a-php-array-into-a-javascript-array) – Jimmy Sawczuk Mar 22 '12 at 20:30
  • that is pretty easy to convert ... do you want to use it via AJAX ?? or you just want to convert PHP array to comparable JavaScript array – Baba Mar 22 '12 at 20:37

3 Answers3

4

You are looking for json_encode: http://www.php.net/manual/en/function.json-encode.php

Only your php array definition looks a bit strange :)

Endijs
  • 550
  • 3
  • 7
2

You'd use json_encode

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
2
$arr = array(
    array('03-22-2012', 1000 ),
    array('03-23-2011', 1170 )
);

echo "var = ".json_encode($arr).";";

Output

var = [["03-22-2012",1000],["03-23-2011",1170]];
max
  • 96,212
  • 14
  • 104
  • 165