marukobotto
marukobotto

Reputation: 771

After getting a php array convert it into javascript

I am fetching results from php via ajax. My result in the serverside looks like this.

Array
(
    [0] => IBM Mainframe
    [1] => Intel
    [2] => MIPS
    [3] => MMIX
    [4] => Computer Science (AP/College Intro)
    [5] => Computer Science (College Advanced)
    [6] => Android Programming
)

I am currently printing it out in console.

Serverside : print_r($result);

Clientside :

success: function(r){
                   console.log(r)
               }

I want to fetch the result and within the success convert it into something like this :

var name = [
        "IBM Mainframe",
        "Intel",
        "MIPS",
        "MMIX",
        "Computer Science (AP/College Intro)",
        "Computer Science (College Advanced)",
        "Android Programming"
]

So I can use that variable later

success : function(r){

..............

var name = ....

}

Upvotes: 0

Views: 57

Answers (1)

Tushar
Tushar

Reputation: 87203

Use json_encode from your server script:

Returns the JSON representation of a value

echo json_encode($result, true);

And on client side in success:

r = JSON.parse(r); // Might not required if dataType set as json
console.log(r); // Use it as array here

Upvotes: 2

Related Questions