Home > Backend Development > PHP Problem > How to convert php variable to string array

How to convert php variable to string array

PHPz
Release: 2023-04-17 14:55:32
Original
573 people have browsed it

In PHP, variables can be of different types, including strings, integers, floating point numbers, Boolean values, etc. Sometimes we need to convert a variable from its original type to string type, or store a set of variables as an array of strings. This article will show you how to convert a PHP variable into a string array.

  1. Using the explode() function

First, we need to understand the explode() function in PHP. This function can split a string into an array according to the specified delimiter. The following is an example:

$input = "apple,banana,orange";
$fruits = explode(",", $input);
print_r($fruits);
Copy after login

The result of executing the above code is as follows:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login

In the above code, we put a string $input according to the comma as the delimiter Split into a string array $fruits.

If we want to convert a variable into a string array, we can use this function. For example, if we want to convert a variable $num that stores integers into a one-bit integer array, we can do this:

$num = 123;
$num_array = str_split($num);
print_r($num_array);
Copy after login

The result of executing this code is as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Copy after login

In the above code, we use the str_split() function in PHP, which splits a string into an array according to the specified length. Because we want to convert an integer to an array of one-bit integers, we convert the $num variable to a string before using this function.

  1. Use preg_split() function

In addition to using the explode() function, we can also use the preg_split() function to split the string according to the regular expression pattern. an array.

For example, we have a string $list that contains multiple names and their ratings, each name and rating are separated by a colon, and we want to combine the name and rating Stored in an array respectively. You can do it like the following:

$list = "apple:8.5,banana:9.2,orange:6.5";
$separated = preg_split("/[:,]/", $list);
print_r($separated);
Copy after login

The result of executing this code is as follows:

Array
(
    [0] => apple
    [1] => 8.5
    [2] => banana
    [3] => 9.2
    [4] => orange
    [5] => 6.5
)
Copy after login

In the above code, we use the preg_split() function and pass a regular expression pattern as delimiter so that names and ratings are stored in separate arrays.

  1. Convert variable to string and use substr() function

We can also convert any type of variable to string and use substr() function to Split it into an array of strings. For example, if we have a numeric variable $num and want to convert it to a one-bit integer array, we can do it as follows:

$num = 123;
$str_num = strval($num);
$num_array = array();
for ($i = 0; $i < strlen($str_num); $i++) {
    $num_array[] = substr($str_num, $i, 1);
}
print_r($num_array);
Copy after login

The result of executing this code is as follows :

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Copy after login

In the above code, we convert the integer variable $num to the string $str_num and use a for loop to iterate over the string and Use the substr() function to put it bit by bit into an array $num_array.

Summary

This article introduces three commonly used methods to convert PHP variables into string arrays. Use these methods to convert variables of different types into string arrays and are ideal for data processing and storage. Whether you're just starting to learn PHP or you already have some experience developing PHP, these methods can help you better handle PHP variables.

The above is the detailed content of How to convert php variable to string 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