Home >Backend Development >PHP Problem >How to convert array in php json

How to convert array in php json

PHPz
PHPzOriginal
2023-04-27 09:06:36651browse

In PHP, JSON is a common data format that can be used to represent structured data. Data in JSON format can be easily transferred and parsed between different systems. In PHP, we can use the following code to convert a JSON string into an array:

$json_str = '{"name":"John","age":30,"city":"New York"}';
$arr = json_decode($json_str, true);

Among them, the json_decode function is used to decode a JSON formatted string into a PHP array. The first parameter is the JSON string to be decoded, and the second parameter is a Boolean value indicating whether to decode the result into an associative array. If set to true, the result will be an associative array; if false or this parameter is omitted, the result will be a plain indexed array.

In addition to converting JSON strings to arrays, we can also convert PHP arrays to JSON-formatted strings:

$arr = array('name' => 'John', 'age' => 30, 'city' => 'New York');
$json_str = json_encode($arr);

Here, the json_encode function is used to convert PHP array is encoded as a string in JSON format, and a string is returned. It should be noted that if the PHP array contains non-UTF-8 encoded strings, you need to convert them to UTF-8 encoding first and then encode them. You can use the mb_convert_encoding function for conversion as follows:

$arr = array('name' => '张三', 'age' => 30, 'city' => '北京');
$arr_utf8 = array_map(function($item){
    return mb_convert_encoding($item, 'UTF-8', 'GBK');
}, $arr);
$json_str = json_encode($arr_utf8, JSON_UNESCAPED_UNICODE);

Here the array_map function is used to iterate through each element in the array and convert it to UTF -8 encoding. The second parameter JSON_UNESCAPED_UNICODE means not to escape the Chinese characters and keep them output as they are.

In addition to converting PHP arrays to JSON strings, you can also convert objects to JSON strings. In this case, the object needs to be serialized, which is usually achieved through the jsonSerialize method:

class Person {
    public $name;
    public $age;
    public $city;
    
    public function __construct($name, $age, $city) {
        $this->name = $name;
        $this->age = $age;
        $this->city = $city;
    }
    
    public function jsonSerialize() {
        return [
            'name' => $this->name,
            'age' => $this->age,
            'city' => $this->city
        ];
    }
}

$person = new Person('John', 30, 'New York');
$json_str = json_encode($person);

Here, the Person class implements jsonSerialize method, which converts the object into an associative array, and then uses the json_encode function to encode it into a JSON format string.

In short, JSON format is widely used in web applications. PHP has built-in support for JSON format, which can easily convert JSON strings into PHP arrays, and convert PHP arrays or objects into JSON format. String.

The above is the detailed content of How to convert array 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