0

if i have an associative array in php.

Say, in the same view, i have $params array:

$parameters = array(
 'parameter1'=>'value1',
 'parameter2'=>'value2
);

Is there any way or php function or even javascript function to convert the php array to javascript array? in such a way that if we have the converted array, jsParameters, you'd be able to access it's fields and values like :

console.log(jsParameters.parameter1);
console.log(jsParameters.parameter2);

that will output, 'value1' and 'value2'. Is there a way? many Thanks!

asked Mar 3, 2014 at 8:00

3 Answers 3

3

Use json_encode:

console.log(<?php echo json_encode($parameters); ?>);
answered Mar 3, 2014 at 8:02
Sign up to request clarification or add additional context in comments.

Comments

2

Use json_encode:

echo json_encode($parameters);
answered Mar 3, 2014 at 8:02

Comments

2

You should be able to call json_encode on the array, and echo that back to your application.

Check out the docs here

answered Mar 3, 2014 at 8:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.