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?

2
  • possible duplicate of Assigning a php array into a javascript array Commented Mar 22, 2012 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 Commented Mar 22, 2012 at 20:37

3 Answers 3

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 :)

2

You'd use json_encode

2
  • i actually need it in the exact format I posted so not in json format no keys no curly braces ...is there an easy way to get this? Thanks!! Commented Mar 22, 2012 at 21:48
  • It just means you need to transform your php array before json_encodeing it. Commented Mar 23, 2012 at 1:27
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]];
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.