Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert POST array to json format

Tags:

json

php

How can I convert php's POST variable to json format?

like image 393
rails_noob Avatar asked Nov 23 '11 07:11

rails_noob


People also ask

How to transform an array into a JSON?

You convert the whole array to JSON as one object by calling JSON. stringify() on the array, which results in a single JSON string. To convert back to an array from JSON, you'd call JSON. parse() on the string, leaving you with the original array.

How to convert array string to JSON?

The JSON. stringify() method converts a JavaScript object, array, or value to a JSON string. If you so choose, you can then send that JSON string to a backend server using the Fetch API or another communication library.

How to convert normal array to JSON in PHP?

To convert PHP array to JSON, use the json_encode() function. The json_encode() is a built-in PHP function that converts an array to json. The json_encode() function returns the string containing a JSON equivalent of the value passed to it, as demonstrated by the numerically indexed array.


2 Answers

If this is entirely in php, and you simply want to convert the data in $_POST to JSON, you can do so like this:

$json = json_encode($_POST);

Using php's built-in function json_encode (doc)

If this is not what you want to do, please be more specific with your question.

like image 88
Code Magician Avatar answered Oct 23 '22 03:10

Code Magician


json_encode($_POST);
  • Docs: http://php.net/manual/en/function.json-encode.php
like image 23
Peter Avatar answered Oct 23 '22 03:10

Peter