Home  >  Article  >  Backend Development  >  How to convert JSON to PHP arrays and objects

How to convert JSON to PHP arrays and objects

PHPz
PHPzOriginal
2023-04-26 14:24:20389browse

PHP is a powerful development language commonly used to build web applications and back-end services. In PHP, the use of JSON (JavaScript Object Notation) is very common because JSON is a data format similar to JavaScript objects and is easy to use and process. During development, you may need to convert JSON data into PHP arrays or objects for easier processing and manipulation. In this article, we'll cover how to convert JSON to PHP arrays and objects and how to use them to build your own applications.

First, let’s take a look at how to convert JSON to a PHP array. PHP provides a function called json_decode() which decodes a JSON string into a PHP array. Here is a simple example:

<?php  
$json = &#39;{"name":"Tom", "age":30, "city":"New York"}&#39;;  
$array = json_decode($json, true);  
print_r($array);  
?>

In this example, we define a JSON string and then use the json_decode() function to decode it into a PHP array. The first parameter of the function is the JSON string to be decoded, and the second parameter is a Boolean value indicating whether the decoded result is an associative array or a normal array. In this example, we set the second parameter to true in order to get an associative array.

When we run the above code, the output will be the following:

Array ( [name] => Tom [age] => 30 [city] => New York )

Now that we have converted the JSON string into a PHP array, we can use the functions provided by PHP array operate.

Next, let’s take a look at how to convert JSON to a PHP object. Similar to converting JSON to a PHP array, PHP also provides a function called json_decode() to decode a JSON string into a PHP object. Here is an example:

<?php  
$json = &#39;{"name":"Tom", "age":30, "city":"New York"}&#39;;  
$obj = json_decode($json);  
echo $obj->name;  
echo $obj->age;  
echo $obj->city;  
?>

In this example, we define a JSON string and use the json_decode() function to decode it into a PHP object. We can then manipulate the object like a normal PHP object. Object properties are accessed using arrow symbols (->) instead of dots (.).

When we run the above code, the output will be the following:

Tom
30
New York

Now that we have converted the JSON string into a PHP object, we can use the methods and properties provided by the PHP object its operation.

Finally, let’s look at how to convert an array or object into a JSON string. PHP provides a function called json_encode() which encodes a PHP array or object into a JSON string. Here is an example:

<?php  
$array = array("name" => "Tom", "age" => 30, "city" => "New York");  
$json = json_encode($array);  
echo $json;  
?>

In this example, we define a PHP array and then encode it into a JSON string using the json_encode() function. We then print that string to the screen using an echo statement.

When we run the above code, the output will be the following:

{"name":"Tom","age":30,"city":"New York"}

Similarly, we can encode the PHP object into a JSON string using the json_encode() function.

In this article, we introduced how to convert JSON to PHP arrays and objects, and how to convert PHP arrays and objects to JSON strings. These will be very useful in web applications and backend services, and for beginners, this is one of the essential skills in PHP development.

The above is the detailed content of How to convert JSON to PHP arrays and objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn