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

How to convert php array to json string array object

WBOY
WBOYOriginal
2023-05-06 12:58:08461browse

In PHP, conversion between arrays and JSON is a very common operation. Arrays are a fundamental data type in PHP, and JSON is a lightweight data exchange format used for Internet communication. Converting an array into a JSON string makes it easy to transmit the data over the network or store it in a file, and it also makes it easier for clients or other applications to obtain and parse the data. This article will introduce in detail how to convert arrays into JSON strings, JSON objects and JSON arrays in PHP.

Basic concept of array

In PHP, an array is a special variable that allows the storage of multiple values. The data in an array can be accessed using numeric or string indexes. For example, the following array:

$myArray = array("apple", "banana", "cherry");

You can use numeric index to access the value:

echo $myArray[0]; // 输出 apple

You can also use string index to access the value:

$myArray = array("fruit1" => "apple", "fruit2" => "banana", "fruit3" => "cherry");
echo $myArray["fruit2"]; // 输出 banana

Change the array Convert to JSON string

PHP provides a very convenient method, json_encode(), which can convert an array into a JSON string. Just pass the array to the json_encode() function, as shown below:

$myArray = array("fruit1" => "apple", "fruit2" => "banana", "fruit3" => "cherry");
echo json_encode($myArray);

The above code will output the following:

{"fruit1":"apple","fruit2":"banana","fruit3":"cherry"}

Convert JSON string to array

If you get a JSON string from other programs or network interfaces and need to convert it into a PHP array, you can use the json_decode() function, as shown below:

$jsonStr = '{"fruit1":"apple","fruit2":"banana","fruit3":"cherry"}';
$myArray = json_decode($jsonStr, true);

The above code will Will convert the JSON string into a PHP array:

Array
(
    [fruit1] => apple
    [fruit2] => banana
    [fruit3] => cherry
)

Note that passing true in the second parameter means that the returned result is an associative array, otherwise the returned result is an object.

Convert an array into a JSON object

If you want to convert an array into a JSON object, consider using the stdClass type. stdClass is a predefined class in PHP that can easily create an empty object. Then assign the data in the array to the properties of the object one by one, and finally encode the object into a JSON string.

$myArray = array("fruit1" => "apple", "fruit2" => "banana", "fruit3" => "cherry");

$jsonObj = new stdClass();
foreach ($myArray as $key => $value) {
    $jsonObj->$key = $value;
}
echo json_encode($jsonObj);

The above code will output the following:

{"fruit1":"apple","fruit2":"banana","fruit3":"cherry"}

Convert array to JSON array

If you want to convert an array to JSON array, it is also very simple. Just add the values ​​in the array to an empty array one by one, and then encode the array into a JSON string.

$myArray = array("apple", "banana", "cherry");

$jsonArray = array();
foreach ($myArray as $value) {
    array_push($jsonArray, $value);
}
echo json_encode($jsonArray);

The above code will output the following:

["apple","banana","cherry"]

Summary

This article introduces how to convert arrays into JSON strings, JSON objects and JSON arrays in PHP. These functions are widely used in web development, API interfaces, and data transmission. During the development process, we need to convert the array into JSON format at any time to facilitate transmission and processing.

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