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

How to convert array object in php json

PHPz
PHPzOriginal
2023-04-26 09:10:12362browse

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.

  1. Convert JSON to array

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
)
  1. Convert JSON to object

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)
}
  1. Convert array to JSON String

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}
  1. Object conversion It is also very simple to convert a PHP object into a JSON string using the built-in
  2. json_encode()
function. Before conversion, you need to use another parameter of the

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;
The above code will output the following easy-to-read JSON formatted string:

{
    "name": "Jack",
    "age": 20
}

Convert array objects to each other

  1. To convert an array containing multiple objects into a string in JSON format, you can use the
  2. json_encode()
function. To convert a JSON-formatted string back into a PHP array object, you can use the

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
    }
]
Next, convert this array containing multiple objects Convert the JSON format string back to a PHP array object:

$jsonStr = '[{"name":"Jack","age":20},{"name":"Tom","age":25},{"name":"Lucy","age":18}]';
$arr = json_decode($jsonStr);
print_r($arr);
Running the above code will output the following PHP array object:

Array
(
    [0] => stdClass Object
        (
            [name] => Jack
            [age] => 20
        )

    [1] => stdClass Object
        (
            [name] => Tom
            [age] => 25
        )

    [2] => stdClass Object
        (
            [name] => Lucy
            [age] => 18
        )

)
Due to the use of the

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 to

true. 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!

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