In web development, the JSON format is very common because it can transmit and exchange data between different programming languages. The PHP language also provides a very convenient JSON encoding and decoding method, which can convert JSON strings into PHP arrays or objects, and also convert PHP arrays or objects into JSON strings. This article will introduce methods in PHP to convert JSON strings into arrays or objects, and further convert arrays or objects into arrays or objects.
Use PHP's built-in json_decode()
function to convert a JSON format string into a PHP array. json_decode()
The function receives two parameters. The first parameter is the JSON format string to be converted. The second parameter is a Boolean value. If this parameter is set to true, the JSON will be decoded. It is an associative array. If this parameter is false, an object is returned. By default this parameter is false.
The following is an example of using the json_decode()
function to convert JSON to an array:
$jsonStr = '{"name":"Jack","age":20}'; $arr = json_decode($jsonStr, true); print_r($arr);
The above code will output an array represented by key-value pairs:
Array ( [name] => Jack [age] => 20 )
If you do not specify the second parameter of the json_decode()
function, a PHP object will be returned. It is very simple to use the json_decode()
function to convert a JSON format string into a PHP object. You only need to pass in the JSON format string.
The following is an example of converting a JSON format string into a PHP object:
$jsonStr = '{"name":"Jack","age":20}'; $obj = json_decode($jsonStr); var_dump($obj);
The above code will output a PHP object:
object(stdClass)#1 (2) { ["name"]=> string(4) "Jack" ["age"]=> int(20) }
Use PHP's built-in json_encode()
function to convert a PHP array into a JSON string. json_encode()
The function receives one parameter, which is the PHP array to be converted. When converting the PHP array into a JSON string, the json_encode()
function will automatically convert the key-value pair into Key-value pairs in JSON string.
The following is an example of converting a PHP array to a JSON string:
$arr = array("name"=>"Jack", "age"=>20); $jsonStr = json_encode($arr); echo $jsonStr;
The above code will output the following string in JSON format:
{"name":"Jack","age":20}
json_encode() function, which is the
JSON_PRETTY_PRINT constant, which is used to format the JSON string into an easy-to-read and more human-friendly format .
Here is an example of converting a PHP object to a JSON string:
class Person { public $name; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $person = new Person("Jack", 20); $jsonStr = json_encode($person, JSON_PRETTY_PRINT); echo $jsonStr;
{ "name": "Jack", "age": 20 }
Convert array objects to each other
json_decode() function. The following is an example of converting an array containing multiple objects into JSON format:
class Person { public $name; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $person1 = new Person("Jack", 20); $person2 = new Person("Tom", 25); $person3 = new Person("Lucy", 18); $arr = array($person1, $person2, $person3); $jsonStr = json_encode($arr, JSON_PRETTY_PRINT); echo $jsonStr;
The above code will output the following string in JSON format:
[ { "name": "Jack", "age": 20 }, { "name": "Tom", "age": 25 }, { "name": "Lucy", "age": 18 } ]
$jsonStr = '[{"name":"Jack","age":20},{"name":"Tom","age":25},{"name":"Lucy","age":18}]'; $arr = json_decode($jsonStr); print_r($arr);
Array ( [0] => stdClass Object ( [name] => Jack [age] => 20 ) [1] => stdClass Object ( [name] => Tom [age] => 25 ) [2] => stdClass Object ( [name] => Lucy [age] => 18 ) )
json_decode()
function The second parameter is not specified, so an array of PHP objects will be returned. If you want to decode JSON to a PHP associative array, you need to set the second parameter totrue.
In this article, we introduced how to convert JSON formatted string to array or object in PHP and further convert array or object to JSON string. Learning these features can help you process JSON data in your web applications more easily.
The above is the detailed content of How to convert array object in php json. For more information, please follow other related articles on the PHP Chinese website!