Home >Backend Development >PHP Problem >How to convert php string to array object array
In PHP, conversion between strings and arrays has always been an important operation. Especially when developing web applications, you often encounter the situation of converting a string to an array or an array to a string. This article will share some operations about converting string arrays and object arrays in PHP.
1. Convert a string into an array
In PHP, use the function explode()
to convert a string into an array. This function splits a string into small pieces and places them in an array. Here is some sample code:
$str = "apple,orange,banana"; $arr = explode(",", $str); print_r($arr);
The output of the above code is:
Array ( [0] => apple [1] => orange [2] => banana )
$str = "the quick brown fox"; $arr = explode(" ", $str); print_r($arr);
The output of the above code is:
Array ( [0] => the [1] => quick [2] => brown [3] => fox )
$str = "path/to/file.txt"; $arr = explode("/", $str); print_r($arr);
The output result of the above code is:
Array ( [0] => path [1] => to [2] => file.txt )
2. Convert the array into a string
In PHP, use the function implode()
Convert the array into a string. This function combines all elements in an array into a string, separating each element using the specified delimiter. Here is some sample code:
$arr = array("apple", "orange", "banana"); $str = implode(",", $arr); echo $str;
The output of the above code is:
apple,orange,banana
$arr = array("the", "quick", "brown", "fox"); $str = implode(" ", $arr); echo $str;
The output of the above code is:
the quick brown fox
$arr = array("path", "to", "file.txt"); $str = implode("/", $arr); echo $str;
The output result of the above code is:
path/to/file.txt
3. Convert the string array into an object array
In PHP, you can usejson_decode()
The function converts a string array into an object array. This function converts a JSON-formatted string into a PHP object or array. The following is some sample code:
$json_str = '[{"name":"apple","color":"red"},{"name":"orange","color":"orange"},{"name":"banana","color":"yellow"}]'; $obj_arr = json_decode($json_str); print_r($obj_arr);
The output of the above code is:
Array ( [0] => stdClass Object ( [name] => apple [color] => red ) [1] => stdClass Object ( [name] => orange [color] => orange ) [2] => stdClass Object ( [name] => banana [color] => yellow ) )
$json_str = '{"name":"apple","color":"red"}'; $arr = json_decode($json_str, true); print_r($arr);
The output of the above code is:
Array ( [name] => apple [color] => red )
The above are some information about strings in PHP Operations for converting arrays and object arrays. These transformation operations are also common tasks in web development. During the development process, depending on the data format, using these operations flexibly can improve the efficiency and readability of the code.
The above is the detailed content of How to convert php string to array object array. For more information, please follow other related articles on the PHP Chinese website!