How to convert string to array and object array in php

PHPz
Release: 2023-04-11 10:00:52
Original
388 people have browsed it

In PHP, we often need to convert a string into an array or object array for operation. This process is very simple. In this article, we will introduce how to convert string to array and object array using PHP.

1. Convert a string into an array

Using PHP's built-in function explode() can convert a string into an array according to the specified delimiter, which is very convenient. The sample code is as follows:

$str = "张三,李四,王五"; $arr = explode(",", $str); print_r($arr);
Copy after login

The output result is as follows:

Array ( [0] => 张三 [1] => 李四 [2] => 王五 )
Copy after login

In the above code, we use "," as the separator to convert a string containing three names into array and output.

2. Convert a string into an object array

Sometimes we need to convert a string into an object array. In this case, we can use PHP's built-in function json_decode(). This function can convert a JSON format string into a PHP object or array, which is very convenient. The sample code is as follows:

$json_str = '[{"name":"张三","age":"20"},{"name":"李四","age":"25"},{"name":"王五","age":"30"}]'; $obj_arr = json_decode($json_str); print_r($obj_arr);
Copy after login

The output result is as follows:

Array ( [0] => stdClass Object ( [name] => 张三 [age] => 20 ) [1] => stdClass Object ( [name] => 李四 [age] => 25 ) [2] => stdClass Object ( [name] => 王五 [age] => 30 ) )
Copy after login

In the above code, we convert a string in JSON format containing three names and ages into an object array and output it.

3. Convert object array to string

PHP provides a built-in function json_encode() that converts object array to JSON format string. We just need to pass the array of objects into the function to convert it to a string. The sample code is as follows:

$person1 = array("name"=>"张三","age"=>"20"); $person2 = array("name"=>"李四","age"=>"25"); $person3 = array("name"=>"王五","age"=>"30"); $obj_arr = array($person1, $person2, $person3); $json_str = json_encode($obj_arr); echo $json_str;
Copy after login

The output result is as follows:

[{"name":"张三","age":"20"},{"name":"李四","age":"25"},{"name":"王五","age":"30"}]
Copy after login

In the above code, we form three arrays containing names and ages into an object array, and then use the json_encode() function to Convert it to JSON format string and output.

4. Summary

Through the above introduction, we learned how to use PHP to convert a string into an array or object array. For some scenarios that require parsing or processing of strings, converting to an array or object array is very convenient. I believe readers will also frequently use these techniques in their daily development work.

The above is the detailed content of How to convert string to array and object array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!