-1

I have the following PHP code which encodes a PHP array:

Code

$js_array = json_encode($tmp);

Output

{
 "frfgt55":["ABC","frfgt55","Aberdeen"],
 "vfrgt6":["ABC","vfrgt6","Birmingham"],
 "vbgtfdh67":["XYZ","vbgtfdh67","Leeds"],
 "vfe5gb":["XYZ","vfe5gb","Bristol"],...
}

What I am struggling with is to then access this within a jQuery script. I know I should be making use of $.getJSON but I am struggling with its implementation as my Ajax knowledge is limited. I cannot see how this would access the variable that as been encoded.

Ajax code

$.getJSON('../_client/index.php', function(data) {
    /* data will hold the php array as a javascript object */
});

Any advice, feedback and comments welcomed.

8
  • @ex3v - the question is clear - how do i access the php array that I have encoded within a jQuery script. I've also added the Ajax code but as my knowledge is limited I am struggling to understand how this works. Commented Jul 8, 2014 at 14:03
  • make sure you output the header('Content-Type: application/json'); before any other output, data should contain data.frfgt55[0] etc.... Commented Jul 8, 2014 at 14:06
  • What's your question then? ^^ Anyways, you cannot access a PHP array via JS: what you do is to make a HTTP request to a server, which will respond with an HTTP response. Browser has no knowledge about how such response was generated, it just parses it [being it a JSON string], and does its business with it. Commented Jul 8, 2014 at 14:08
  • @moonwave99 - 'Access PHP Array within jQuery (Ajax, JSON)' - question is pretty clear. Commented Jul 8, 2014 at 14:15
  • @Homer_J what is your problem then? Did your attempt work? If not, what errors did you get? Commented Jul 8, 2014 at 14:17

2 Answers 2

4

If I well understood your needs you can access data with:

$.getJSON('../_client/index.php', function(data) {
    /* data will hold the php array as a javascript object */
    console.info(data.frfgt55); //accessing the first item of the array
});

EDIT

In order to handle success and error code I suggest you to use the promise interface and so replace the current $.getJSON() code with:

$.getJSON('../_client/index.php')
.success(function(response) { console.info(response); alert("success"); })
.fail(function(jqXHR, status, error){ console.info(error); alert("error"); });
Sign up to request clarification or add additional context in comments.

8 Comments

It should be data.frfgt55[0] unless you want the entire first array.
It could be everything...I've just given @Homer_J a response in order to help him find his way, since he didn't know how to access data
Thanks for this - added to my code and also added alert(data.frfgt55); but no output at all.
Hmm what's the console output of console.info(data); ? In case it's empty then probably you're not echoing the response array? just my assumption..
It's not displaying any data (removed the previous error that was linked to other code)
|
0

If I am correct then check this code it will help you

$.getJSON('../_client/index.php', function(data) {
    for(var key in data) { // In comment check first loop data for each record.
       // key = 'frfgt55'; // For 1st loop
        var yourArrayRecord = data[key]; // For 1st loop <- ["ABC","frfgt55","Aberdeen"]
        var perticularValue1 = yourArrayRecord[0]; // For 1st loop <- "ABC"
        var perticularValue2 = yourArrayRecord[1]; // For 1st loop <- "frfgt55"
        var perticularValue3 = yourArrayRecord[2]; // For 1st loop <- "Aberdeen"
    }
});

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.