How to convert php string to object array

PHPz
Release: 2023-04-17 14:37:32
Original
664 people have browsed it

In the process of developing a website, we often need to convert strings into object arrays to facilitate data operations. In PHP, there are many ways to achieve this function, and this article will introduce one of them to you.

1. Use the json_decode function

PHP provides the json_decode function, which can convert JSON format strings into objects or arrays. The following is a simple example:

<?php
$str = &#39;{"name": "Tom", "age": 18}&#39;;
$obj = json_decode($str);
print_r($obj);
?>
Copy after login

The output is:

stdClass Object
(
    [name] => Tom
    [age] => 18
)
Copy after login

As you can see, the json_decode function converts the JSON string into an object named stdClass.

If you want to convert it into an array, you can add the second parameter true to the function, as shown below:

<?php
$str = &#39;{"name": "Tom", "age": 18}&#39;;
$arr = json_decode($str, true);
print_r($arr);
?>
Copy after login

The output result is:

Array
(
    [name] => Tom
    [age] => 18
)
Copy after login

You can see Now, the json_decode function converts the JSON string into an associative array.

2. Split the string by lines and convert it into an array

If your string is not in JSON format, you can split it by lines first and then process it line by line. The following is a simple example:

<?php
$str = "Tom,18\nJerry,22\n";
$arr = explode("\n", $str);
foreach($arr as $item) {
    $tmp = explode(",", $item);
    $result[] = array(
        "name" => $tmp[0], 
        "age" => $tmp[1]
    );
}
print_r($result);
?>
Copy after login

The output result is:

Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 18
        )

    [1] => Array
        (
            [name] => Jerry
            [age] => 22
        )

)
Copy after login

As you can see, after splitting the string by lines, it is processed line by line and converted into an object containing multiple objects. array.

3. Use regular expressions

If your string format is more complex, or you need to perform more complex processing on the string, you can use regular expressions. The following is a simple example:

<?php
$str = "name=Tom&age=18&gender=male";
preg_match_all("/(\w+)=([^&]+)/", $str, $matches);
foreach($matches[1] as $key => $value) {
    $result[$value] = $matches[2][$key];
}
print_r($result);
?>
Copy after login

The output result is:

Array
(
    [name] => Tom
    [age] => 18
    [gender] => male
)
Copy after login

As you can see, the query string is converted into an associative array using regular expressions.

The above are three methods of converting strings into object arrays. Readers can choose the appropriate method according to the actual situation.

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