Home  >  Article  >  Backend Development  >  How to convert string to json object array in php

How to convert string to json object array in php

PHPz
PHPzOriginal
2023-04-27 15:54:29502browse

PHP is a very popular server-side scripting language. Like other programming languages, it can handle multiple data types such as strings, arrays, and objects. In modern web development, using the JavaScript Object Notation (JSON) format to transmit data has become a very popular way because it is compact and easy to work with. In this article, we will learn how to convert strings to JSON objects and arrays in PHP.

First, let us introduce JSON. It is a lightweight data exchange format used to represent simple data types (strings, numbers, Boolean values, and null values) and complex data types (objects and arrays). Data in JSON format is composed of key-value pairs, where the key is a string and the value can be any JSON data type.

PHP provides some built-in functions that can help us convert JSON data to string or string to JSON data. Among them, the json_encode() function is used to convert PHP objects or arrays into JSON-formatted strings, and the json_decode() function is used to convert JSON-formatted strings into PHP objects or arrays. In this article, we will focus on how to use the json_encode() function to convert a string into a JSON object or array.

Convert a string to a JSON object:

First, we need to create an associative array containing key-value pairs and then use the json_encode() function to convert it to a JSON object. The following is a simple example:

<?php
    $str = &#39;{"name": "John", "age":30, "city":"New York"}&#39;;
    $json_obj = json_decode($str);
    var_dump($json_obj);
?>

In this example, we first define a string variable $str, which contains a list of key-value pairs of a JSON object. We then use the json_decode() function to convert that string into a PHP object or array. Finally, we use the var_dump() function to output the converted JSON object.

Convert a string to a JSON array:

Similar to converting a string to a JSON object, we also need to use the json_encode() function to convert a string to a JSON array. Here is an example:

<?php
    $str = &#39;["apple", "banana", "orange"]&#39;;
    $json_arr = json_decode($str);
    var_dump($json_arr);
?>

In this example, we define a variable $str that contains a string, which represents a JSON array containing three fruits. We then use the json_decode() function to convert that string into a PHP object or array. Finally, we use the var_dump() function to output the converted JSON array.

Summary:

In this article, we introduced how to convert strings to JSON objects and arrays using php. Whether you are using the json_encode() function to convert a PHP object or array to a JSON-formatted string, or using the json_decode() function to convert a JSON-formatted string to a PHP object or array, these functions are very useful. Using JSON format to transmit data is a very popular way in web development. Due to its simplicity and tractability, the importance of using it to transmit and store data cannot be ignored.

The above is the detailed content of How to convert string to json object array in php. 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