Home  >  Article  >  Backend Development  >  Discuss how to convert php array into json string

Discuss how to convert php array into json string

PHPz
PHPzOriginal
2023-04-11 09:16:35408browse

With the continuous development of Internet technology, Javascript has become a "hardcore loyalist" of web clients and is widely used in major browsers. In this case, Javascript-related technologies are becoming more and more worthy of attention. Among them, Json, as a lightweight data exchange format, has been widely used in data transmission between front-end and back-end. It not only facilitates data transmission, but also reduces network data traffic.

PHP has good support for Json format and can easily convert arrays into Json format strings. In this article, we will discuss how to convert PHP array into Json string.

1. PHP array format

In PHP language, array is a very common data structure that can store multiple variable data. Generally speaking, arrays in PHP have two formats:

1. Index array: a set of data with numbers as keys;

2. Associative arrays: strings or numbers as keys A set of data for key.

For example, the following is an example of a PHP array:

<?php
    //索引数组
    $arr1 = array("Hello","World","Php");
    //关联数组
    $arr2 = array(
        "name" => "Tom",
        "age" => 18,
        "sex" => "male"
    )
?>

2. How to convert a PHP array into a Json string

In PHP, convert the array into a Json character Strings are mainly implemented through the json_encode () function. The json_encode () function can convert a PHP array into a Json format string. The following is a code example after adding the function:

<?php
    //1.索引数组转换成Json格式字符串
    $arr1 = array("Hello","World","Php");
    $json1 = json_encode($arr1);
    echo $json1; //["Hello","World","Php"]

    //2.关联数组转换成Json格式字符串
    $arr2 = array(
        "name" => "Tom",
        "age" => 18,
        "sex" => "male"
    );
    $json2 = json_encode($arr2);
    echo $json2; //{"name":"Tom","age":18,"sex":"male"}
?>

We can see that the json_encode () function can convert a PHP array into a string in Json format, and automatically handle various punctuation marks, escape characters and other symbols , ensure that the generated string conforms to the Json format specification.

3. Method of converting Json string into PHP array

Similarly, we can also convert Json format string into PHP array. In PHP, there is a json_decode () function that does this. The following is a code example after adding the json_decode() function:

<?php
    $arr = array(
        "name" => "Tom",
        "age" => 18,
        "sex" => "male"
    );
    $json = json_encode($arr);
    var_dump($json); //string(31) "{"name":"Tom","age":18,"sex":"male"}"

    //Json字符串转换成PHP数组
    $arr_json = json_decode($json,true);
    var_dump($arr_json); //array(3) { ["name"]=> string(6) "Tom" ["age"]=> int(18) ["sex"]=> string(4) "male" }
?>

In the above code, the var_dump() function is used for debugging, so that we can view the value and data type of each variable.

Summary:

The above is the description of how to convert PHP arrays into Json strings in this article. We can see that PHP’s built-in json_encode() and json_decode() functions are very convenient. You can convert PHP arrays to Json format strings or Json format strings to PHP arrays. This is very beneficial for data transmission and processing, and also provides great convenience to developers.

The above is the detailed content of Discuss how to convert php array into json string. 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