How to return an array from an AJAX call

0 votes

I'm searching for a better solution to making an AJAX call with jQuery, having the PHP file return an array, and have it come out client-side as a Javascript array. Here is what I have been doing this:

PHP File (Example.php):

<?php
    $id_numbers = array('NES-ZL','NES-AL','SNS-ZL');

    for ($i=0; $i<count($the_array); $i++){
        echo $id_numbers[$i];
        echo '|';
    }
?>

JS File:

id_numbers = new Array();
$.ajax({
    url:"Example.php",
    type:"POST",
    success:function(msg){
        id_numbers = msg.split('|');
    }
});

What I'd like to do is to be able to just

return $id_numbers;

on the PHP side and directly translate it to a Javascript array after the AJAX call.

Any help?

Nov 14, 2020 in PHP by kartik
• 37,520 points
17,300 views

1 answer to this question.

0 votes

Hello @kartik,

Use JSON to transfer data types (arrays and objects) between client and server.

In PHP:

  • json_encode
  • json_decode

In JavaScript:

  • JSON.stringify
  • JSON.parse

PHP:

echo json_encode($id_numbers);

JavaScript:

id_numbers = JSON.parse(msg);

Hope it helps!!

answered Nov 14, 2020 by Niroj
• 82,880 points

Related Questions In PHP

0 votes
1 answer

How to store values from foreach loop into an array?

Hello @kartik, Declare the $items array outside the loop and ...READ MORE

answered Sep 1, 2020 in PHP by Niroj
• 82,880 points
29,989 views
0 votes
1 answer

How to remove duplicate values from an array in PHP?

Hello @kartik, Use array_unique(): Example: $array = array(1, 2, 2, 3); $array ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,880 points
2,719 views
0 votes
1 answer

How to load return array from a PHP file?

Hii @kartik, When an included file returns something, ...READ MORE

answered Nov 10, 2020 in PHP by Niroj
• 82,880 points
3,160 views
0 votes
2 answers

How to override trait function and call it from the overridden function?

instead of: return traitcalc($v); use this: ...READ MORE

answered Dec 13, 2020 in PHP by Uncle Nick
3,595 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,957 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,969 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,900 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,397 views
0 votes
1 answer

How to return an array from an AJAX call?

Hello, Php has a function for this, just ...READ MORE

answered Nov 8, 2020 in PHP by Niroj
• 82,880 points
3,058 views
0 votes
1 answer

How to call a php function from ajax?

Hello @kartik, You can't call a PHP function ...READ MORE

answered Jun 16, 2020 in PHP by Niroj
• 82,880 points
12,736 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP