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

How to convert json string array to php string array

PHPz
PHPzOriginal
2023-03-28 16:18:34381browse

In PHP development, we often need to convert JSON string array to string array. This process can be implemented using PHP's built-in json_decode() function. In this article, we will focus on how to convert a JSON string array into a string array using the json_decode() function.

  1. What is a JSON string array?

JSON (JavaScript Object Notation) is a lightweight data exchange format commonly used to receive and send data from a server asynchronously. A JSON string array is an array composed of JSON objects. Each JSON object is a string containing key-value pairs. For example:

[
    {"name": "John", "age": 30},
    {"name": "Jane", "age": 25},
    {"name": "Bob", "age": 40}
]

In the above JSON string array, there are three JSON objects, each JSON object contains two key-value pairs "name" and "age". Now we need to convert this JSON string array into a string array.

  1. Use json_decode() to convert JSON string array to PHP array

In PHP, we can use json_decode() function to convert JSON Convert string to PHP array. Since the JSON string array is composed of multiple JSON objects, when converting, you need to set the second parameter to true in order to convert the JSON string array into a PHP associative array. The sample code is as follows:

$json_str = '[
                {"name": "John", "age": 30},
                {"name": "Jane", "age": 25},
                {"name": "Bob", "age": 40}
            ]';

$arr = json_decode($json_str, true);

In the above code, we store the JSON string array in the $json_str variable and convert it into a PHP array using the json_decode() function. It is important to note that we passed true in the last parameter, which means to convert the JSON string array to a PHP associative array instead of the default PHP object.

Now, we can use the print_r() function to print out the contents of the $arr array to see if the JSON string array is converted to a PHP array correctly. The sample code is as follows:

print_r($arr);

Run the above code, you can get the following output:

Array
(
    [0] => Array
        (
            [name] => John
            [age] => 30
        )

    [1] => Array
        (
            [name] => Jane
            [age] => 25
        )

    [2] => Array
        (
            [name] => Bob
            [age] => 40
        )

)

As you can see from the above output, we have successfully converted the JSON string array to a PHP array , and convert each JSON object into a PHP associative array.

  1. Convert PHP array to string array

Now that we have converted the JSON string array to a PHP array, next we need to convert Convert PHP array to string array. This process is relatively simple, just use PHP's built-in array_column() function. The array_column() function can extract specified columns from a PHP array and then form it into a new array. The sample code is as follows:

$str_arr = array_column($arr, 'name');

In the above code, we use the array_column() function to extract the values ​​of all "name" keys in the $arr array and form a new array $str_arr.

Now we can use the print_r() function to print out the contents of the $str_arr array to see if the final result is correct. The sample code is as follows:

print_r($str_arr);

Run the above code, you can get the following output:

Array
(
    [0] => John
    [1] => Jane
    [2] => Bob
)

As you can see from the above output, we have successfully converted the JSON string array to a string array.

  1. Summary

#In this article, we introduced how to convert a JSON string array into a PHP array using PHP’s built-in json_decode() function , and convert the PHP array to a string array using PHP's built-in array_column() function. After studying this article, I believe that readers have mastered how to convert a JSON string array into a string array.

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