algorithmicCoder
algorithmicCoder

Reputation: 6789

Convert array from php to javascript?

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?

Upvotes: 1

Views: 139

Answers (3)

max
max

Reputation: 101811

$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]];

Upvotes: 2

Endijs
Endijs

Reputation: 550

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

Upvotes: 4

zzzzBov
zzzzBov

Reputation: 179046

You'd use json_encode

Upvotes: 2

Related Questions