How to Return JSON from PHP to JavaScript

0 votes

I have a PHP script that's being called through jQuery AJAX. I want the PHP script to return the data in JSON format to the javascript. Here's the pseudo code in the PHP script:

$json = "{";
foreach($result as $addr)
{
    foreach($addr as $line)
    {
        $json .= $line . "\n";
    }
    $json .= "\n\n";
}
$json .= "}";

Basically, I need the results of the two for loops to be inserted in $json. How to do that?

Jul 6, 2020 in Web Development by kartik
• 37,510 points
1,463 views

1 answer to this question.

0 votes

Hello @kartik,

Php has an inbuilt JSON Serialising function.

json_encode

Use that if you can and don't suffer not invented here syndrome.

json_encode

(PHP 5 >= 5.2.0, PHP 7, PECL json >= 1.2.0)

json_encode — Returns the JSON representation of a value

Description

json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ) : string

Returns a string containing the JSON representation of the supplied value.

The encoding is affected by the supplied options and additionally the encoding of float values depends on the value of serialize_precision.

Hope it helps!!

Thank you!!

answered Jul 6, 2020 by Niroj
• 82,880 points

Related Questions In Web Development

0 votes
0 answers

jQuery JSON Decode ( PHP to Javascript)

I'm trying to make an autocomplete script. ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,260 points
405 views
0 votes
0 answers

How to filter JSON Data in JavaScript or jQuery?

How to filter JSON data using Javascript ...READ MORE

Jul 1, 2022 in Web Development by rajatha
• 7,640 points
2,240 views
0 votes
0 answers

jQuery + JSON how to define key from variable

I have the following code $.post( ...READ MORE

Jul 28, 2022 in Web Development by gaurav
• 23,260 points
327 views
0 votes
1 answer

How to upload zip from the S3 bucket to lambda function using AWS CLI

Hi@Abhishek, You want to upload your zip file ...READ MORE

answered Apr 10, 2020 in Web Development by MD
• 95,440 points
5,392 views
0 votes
1 answer

How can we Create Multiple Where Clause Query Using Laravel Eloquent?

Hii, You can use Conditions using Array: $users = User::where([ ...READ MORE

answered Mar 30, 2020 in Laravel by Niroj
• 82,880 points
16,178 views
0 votes
1 answer

How to send email using php?

Hello @kartik 1.) Download PHPMailer, open the zip file ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,880 points
1,020 views
0 votes
1 answer

Where to register Facades & Service Providers in Lumen?

Hello, To register a facade with an alias, ...READ MORE

answered Apr 6, 2020 in Laravel by Niroj
• 82,880 points
4,078 views
0 votes
1 answer

How can I update NodeJS and NPM to the next versions?

Hello @kartik, First check your NPM version npm -v 1).Update ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
759 views
+1 vote
1 answer

How to access the Angularjs scope of a particular html element from our console?

Hello, You should follow the below steps:-- 1.Compile and ...READ MORE

answered Jan 21, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 2,444 views
0 votes
1 answer

How to track with Google Analytics on a redirection page with PHP?

Hello @kartik, Since the page that is sending ...READ MORE

answered Jul 7, 2020 in Web Development by Niroj
• 82,880 points
1,567 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