1

I have a php array with key, value pairs. I have json encoded it. And using ajax.Request i called the php file in which that array is there. Now i have to access both key and value pairs. Could anyone let me know how to do that?

3 Answers 3

1

You need to parse the JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

Don't use eval. It is slow and inefficient. Crockford's json2.js will use faster, native JSON parsers if available.
@David That's why I placed it last :)
0

Code in javascript "json.js" is required. you can download this.

var votedCommentString = getCookie("votedComment"); // get value of voted comment cookie 

votedComment = JSON.parse(votedCommentString); // extract json object into array

votedComment[votedComment.length] = id; // add more data in array

var cookieData = JSON.stringify(votedComment); // convert array into json object

setCookie("votedComment", cookieData, 365); // and again set cookie

In PHP you can access this in following way

$cookieData = (array)json_decode($_COOKIE["votedComment"]); // extract json object into array

print_r($cookieData);

Use

json_encode($array_variable) // to convert array in to json

Comments

0

As you said the array is in php file which is called via ajax, you can simply decode the json encoded string and retrieve the array with keys and values respectively.

Simply use json_decode() function to retrieve the array.

Comments

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.