1

I have an array JSON encode respond when the ajax JSON throw a post request (refer below).

requestparser.php:

$array = array("phweb" => "yes", "phemail" => "yeeess");
echo json_encode($array);

And this Ajax JSON use for sending post request to requestparser.php and processing the return response.

$.ajax({
        type: 'POST',
        url: 'requestparser.php',
        data: { "request" : "pull" },
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        cache: false,
        success: function(result) {
        alert(result[0]);
        alert(result[1]);
        }
});

I want to get the value of array key phweb and the value of array key phemail yet when an alert box popup, it says undefined. What seems the problem? Any help, ideas, clues would be greatly appreciated.

So far what I tried is:

$.ajax({
    type: 'POST',
    url: 'requestparser.php',
    data: { "request" : "pull" },
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    cache: false,
    success: function(result) {
    alert(result[0]->phweb);
    alert(result[1]->phemail);
    }
});

And sadly, it doesn't work.

1 Answer 1

1

The result is a JSON object. You can access it like this

success: function(result) {
    alert(result['phweb']);
    alert(result['phemail']);
}
Sign up to request clarification or add additional context in comments.

7 Comments

please mark it as answered it your question is solved
how about if multidimensional arrays (arrays inside an arrays)? like this. " $array = array("phweb" => array("ph" => $status, "phemail" => $status2)); echo json_encode($array);" how to do it then?
you can do result['phweb']['ph']
how about get the array key? how to do that?
result is the object that holds the phweb and phemail; It just had a key and a value. result : { phweb: '---', 'phemail': '--'} this is how it looks. So it won't have key. Result itself is a key
|

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.