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:
- Convert a comma-separated string into an array:
$str = "apple,orange,banana"; $arr = explode(",", $str); print_r($arr);
The output of the above code is:
Array ( [0] => apple [1] => orange [2] => banana )
- Convert a space-delimited string into an array:
$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 )
- Convert a slash-delimited string Into an array:
$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:
- Convert the array into a comma-separated string:
$arr = array("apple", "orange", "banana"); $str = implode(",", $arr); echo $str;
The output of the above code is:
apple,orange,banana
- Convert the array into a space-separated string:
$arr = array("the", "quick", "brown", "fox"); $str = implode(" ", $arr); echo $str;
The output of the above code is:
the quick brown fox
- Convert the array into a slash-separated string String:
$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:
- Convert a string represented in JSON format into an object array:
$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 ) )
- Convert a string represented in JSON format into an associative array:
$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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
