Skip to content Skip to sidebar Skip to footer

How Do You Assign A Php Array To Jquery Array?

I am unable to figure out how to assign PHP array to jQuery array?. I want to do something like the following: var jQueryArray = ; Can anyone tell me how

Solution 1:

Use json encode.

json_encode — Returns the JSON representation of a value

Example:

var arrayFromPHP = <?phpecho json_encode($arr); ?>;

Solution 2:

You need to use json_encode

var jQueryArray = <?phpecho json_encode($phpArray); ?>;

Solution 3:

You could use the json_encode function:

var jQueryArray = <?phpecho json_encode($phpArray); ?>;

Solution 4:

You can use json_encode

<?php$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

Solution 5:

Don't forget that PHP json_encode will only work on UTF8 encoded text ...

$jsonString = json_encode(array_map(utf8_encode, $rawArray));

would be a more universal solution I think, but I'm a bit tired so 'scuse any coding gaffs ...

Post a Comment for "How Do You Assign A Php Array To Jquery Array?"