1

I am trying to parse an associated array returned from a PHP functoin like this:

$.ajax({
        type: "GET",
        url: "../controllers/Checkpoint.php/getAllCheckpoints"
    }).done(function( result ) {
        result.foreach(function(index, value){
            alert(index + "  "+ value);
        });
    }).error(function(){

    });

i return the array like this in php

foreach($result as $row) {
            $checkpoints[$row['ID']] = $row['Name'];
        }
        return json_encode($checkpoints);

the result of that array reading from the chrome F12 console is

{"1":"check point 1","2":"check point 2"}

I got this error in the javascript code:

ReferenceError: forEach is not defined

could you help me please

5
  • 6
    Your code has foreach (lower e), but your error message has forEach (upper E). Which did you actually use? Commented Dec 11, 2014 at 17:07
  • 1
    by the way, {"1":"check point 1","2":"check point 2"} is not an array. Commented Dec 11, 2014 at 17:15
  • @SultanBaby It's not, and OP does call it that, but OP also called it an associative array, which is correct. This is an unfortunate clashing of PHP and JS terminology (personally I hate the term associative array, since it confuses people with terms for objects and normal arrays). Commented Dec 11, 2014 at 17:18
  • 1
    AFAIK, 'associated' is different from 'associative'. But most importantly, OP's recount of the issue is inconsistent as you already noted - there's no way one would use foreach but then get a mention of forEach in the error message. Commented Dec 11, 2014 at 17:23
  • @SultanBaby Good catch, I read "associated" as "associative". That is clearly incorrect. Commented Dec 11, 2014 at 17:25

4 Answers 4

2

I would suggest using the jQuery.each() generic iterator function in combination with the javascript JSON.parse() to accomplish what you are attempting to do.

jQuery.each()

JSON.parse

.done(function( result ) {
    checkpoints = JSON.parse(result);
    $.each( checkpoints, function( index, value) {
         alert(index + "  "+ value);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

On client side

you should use object.forEach() function e capital letter or jQuery function $.each(object)

    result.forEach(function(index, value){
        alert(index + "  "+ value);
    });

If you have not set response header Content-type: application/json then you will need to parse response content as JSON object use JSON.parse() function

.done(function( result ) {

    JSON.parse(result).forEach(function(index, value){
        alert(index + "  "+ value);
    });
})

On Server side

return mean function that return and assign value into any variable, AJAX response need output buffer so echo content on server side and terminate script after output buffer

    foreach($result as $row) {
        $checkpoints[$row['ID']] = $row['Name'];
    }
    //return json_encode($checkpoints);
    header('Content-type: application/json');
    echo json_encode($checkpoints);
    exit();

header('Content-type: application/json'); means response content type as json object

2 Comments

For older browsers that don't have JSON, there's also jQuery.parseJSON.
yes @ajp15243 may be required, thanks for suggestion
1

If you have the data on client, on browser then Php is no is problem ok. When you is on client. Javascript native isnt have function foreach, You can use $.each function

$.ajax({
    type: "GET",
    url: "../controllers/Checkpoint.php/getAllCheckpoints"
}).done(function( result ) {
    $.each(result, function(i, data){
        console.log(i, data); // this print on console the data that you is run
        // for this you should have installed firebug on firefox or chrome already installed some similar.
    });
}).error(function(){

});

Comments

0

At first: You have to return the php result with "echo" and not with "return".

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.