Home > Article > Backend Development > Convert multiple json to array in php
In PHP development, we usually use Json format (JavaScript Object Notation), which is a lightweight data exchange format that expresses concepts in a form that is easy for people to read and is also suitable for machine parsing. and generate. In actual development, we may encounter situations where we need to convert multiple Json data into PHP arrays, so today we will talk about how to implement this function.
1. Convert a single Json into an array
In PHP, we use the json_decode() function to convert Json format data into a PHP array, such as:
$json_str = '{"name":"John", "age":30, "city":"New York"}'; $arr = json_decode($json_str, true);
Among them, The second parameter true means converting Json data into an associative array. If the second parameter is not added, an object will be generated.
2. Convert multiple Json data into arrays
If you need to convert multiple Json data into PHP arrays, we can use loop traversal to convert the Json data into arrays one by one and merge them. into an array, the code is as follows:
$json_arr = array( '{"name":"John", "age":30, "city":"New York"}', '{"name":"Jane", "age":25, "city":"Los Angeles"}', '{"name":"Tom", "age":40, "city":"Chicago"}' ); $data = array(); foreach($json_arr as $json_str) { $arr = json_decode($json_str, true); // 将Json数据转成数组 if(is_array($arr)) { // 如果转换成功,则将其合并到$data数组中 $data = array_merge($data, $arr); } } print_r($data);
In the above code, we first define an array $json_arr containing multiple Json data, then convert each Json data into an array through loop traversal, and finally convert them Merged together to generate an array $data containing all data.
It should be noted that if Json data fails to be converted into an array, it will not be merged into the $data array to avoid data errors.
3. Use the array_map() function to convert data
In addition to loop traversal, we can also use PHP's array_map() function to convert multiple Json data into arrays. The code is as follows:
$json_arr = array( '{"name":"John", "age":30, "city":"New York"}', '{"name":"Jane", "age":25, "city":"Los Angeles"}', '{"name":"Tom", "age":40, "city":"Chicago"}' ); // 将Json数据转换成数组 $callback = function($json_str) { $arr = json_decode($json_str, true); if(is_array($arr)) { return $arr; } }; $data = array_filter(array_map($callback, $json_arr)); // 过滤掉转换失败的数据 print_r($data);
In the above code, we define a $callback function to convert Json data into an array. This is then passed as a parameter to the array_map() function to transform each element in the array $json_arr. Finally, use the array_filter() function to filter out the data that failed to be converted, and get an array $data containing all the data.
Summary:
Through the above two methods, we can realize the function of converting multiple Json data into PHP arrays. In actual development, if you need to process a large amount of Json data, it is recommended to use the array_map() function because it is faster and the code is simpler. During use, you also need to pay attention to verifying the data to avoid data errors.
The above is the detailed content of Convert multiple json to array in php. For more information, please follow other related articles on the PHP Chinese website!