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,520 points
715 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,760 points

Related Questions In Web Development

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,220 points
3,476 views
0 votes
1 answer

How to get value of HTML Checkbox from onclick/onchange events?

Hello @kartik, Use the click event, which won't fire until ...READ MORE

answered Oct 7, 2020 in Web Development by Niroj
• 82,760 points
1,074 views
+1 vote
2 answers

How to embed Google map on my website?

1. Search whatever you want to embed. 2. ...READ MORE

answered Jan 17, 2020 in Web Development by anonymous
223 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,760 points
8,034 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,760 points
280 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,760 points
1,541 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,760 points
172 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,760 points

edited Jan 21, 2020 by Niroj 189 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,760 points
563 views