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

How to convert php string to array type

PHPz
PHPzOriginal
2023-04-25 09:01:191303browse

In PHP development, converting strings into arrays is a very common task. Usually, we need to convert strings into arrays for easier processing and management.

PHP provides various built-in functions to help us convert strings into arrays. Let's look at some implementation methods.

Method 1: Use the explode function

The explode function is one of the most commonly used methods in PHP to convert a string into an array. It splits the string into array elements using the specified delimiter and returns the resulting array.

For example, we have a comma-delimited string: $str = "apple,orange,banana,grapes";

We can use the explode function to convert the string into an array, as follows Shown:

$arr ​​= explode(',', $str);

The first parameter of this function is the delimiter used to split the string, and the second parameter is The string to be split.

Now, we can use the print_r function to output the elements of the array to check the result:

print_r($arr);

// Output:

/ /

// Array

// (

// [0] => apple

// [1] => orange

// [2] => banana

// [3] => grapes

// )

In the above example, We checked the $arr array and made sure it contained the correct elements.

Some things to note:

  • If the delimiter is at the beginning or end of the string, then the first or last element of the array will be the empty string.
  • If the string contains multiple adjacent delimiters, then an empty string will be added as an element in the array, even if nothing appears.

Method 2: Use the str_split function

The str_split function decomposes the string into an array of specified length. If the split length is not specified, it defaults to 1. The following is an example:

$str = "Hello world!";

$arr ​​= str_split($str);

We can use the print_r function to output the array elements and Check result:

print_r($arr);

// Output:

//

// Array

// (

// [0] => H

// [1] => e

// [2] => l

// [3] => l

// [4] => o

// [5] =>

// [6] => w

// [7] => o

// [8] => r

// [9] => l

// [10] => d

// [11] => !

// )

In the above example, we Saw an issue where the space character is also treated as an array element.

Method 3: Use str_word_count function

The str_word_count function is a PHP built-in function that can break a string into words and then store them in an array. Here is an example:

$str = "The quick brown fox jumps over the lazy dog.";

$arr ​​= str_word_count($str, 1);

We still use the print_r function to output the array elements and check the result:

print_r($arr);

// Output:

//

// Array

// (

// [0] => The

// [1] => quick

// [2 ] => brown

// [3] => fox

// [4] => jumps

// [5] => over

// [6] => the

// [7] => lazy

// [8] => dog

// )

In the above example, we can see that the second parameter of the function specifies that the output result is in the form of an array. Without this parameter, the number of words will be output.

Summary:

In PHP development, we often need to convert strings into arrays for easier processing and management. In this article, we introduce three different ways to implement this process, including using the explode function, str_split function, and str_word_count function. No matter which method you choose, you should choose the method that suits your specific application in order to ultimately achieve a successful project.

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