Home  >  Article  >  Backend Development  >  How to convert php string to array object array

How to convert php string to array object array

PHPz
PHPzOriginal
2023-04-25 09:05:43408browse

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:

  1. 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
)
  1. 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
)
  1. 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:

  1. 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
  1. 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
  1. 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:

  1. 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
        )

)
  1. 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!

Statement:
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