Current Location: Home> Latest Articles> Split the string into an index array and merge it with str_split

Split the string into an index array and merge it with str_split

M66 2025-05-12

In PHP, we can use the str_split function to split the string into an index array, and then combine the two arrays into an array of key-value pairs through the array_combine function. This operation is very useful, especially when certain data needs to be split and merged by specific rules. This article will explain in detail how to use these two functions to achieve this goal.

1. Introduction to str_split function

The str_split function can break a string into an array, each character as an element of the array. The basic syntax of this function is as follows:

 str_split(string $string, int $length = 1): array
  • $string : The string to be split.

  • $length : The maximum length of each array element. The default value is 1.

If the $length parameter is not specified, str_split will return an array of each character as an array element.

Example:

 $string = "hello";
$array = str_split($string);
print_r($array);

Output:

 Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)

2. Introduction to array_combine function

The array_combine function is used to combine two arrays into an associative array, the first array is used as the key and the second array is used as the value. The syntax is as follows:

 array_combine(array $keys, array $values): array
  • $keys : an array used as keys.

  • $values : an array used as values.

If the number of elements of the two arrays is not equal, array_combine will return false .

Example:

 $keys = ["a", "b", "c"];
$values = [1, 2, 3];
$result = array_combine($keys, $values);
print_r($result);

Output:

 Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

3. Combining str_split and array_combine to implement key-value pair merging

We can combine str_split and array_combine , first split a string into an array, and then use another array (such as the number corresponding to the letter) as the value, and merge them into an array of key-value pairs through array_combine .

Sample code:

Suppose we have a string "abcdef" and a corresponding array of numbers [1, 2, 3, 4, 5, 6], and we want to combine letters as keys and numbers as values ​​into an array of key-value pairs.

 <?php
// Define a string
$string = "abcdef";

// Split a string into an array
$keys = str_split($string);

// Define an array of numbers corresponding to a character array
$values = [1, 2, 3, 4, 5, 6];

// use array_combine Merge two arrays
$combined = array_combine($keys, $values);

// Output result
print_r($combined);
?>

Output:

 Array
(
    [a] => 1
    [b] => 2
    [c] => 3
    [d] => 4
    [e] => 5
    [f] => 6
)

As shown above, using str_split to split the string into single characters, then combining them with the number through array_combine to form a complete array of key-value pairs.

Things to note when handling URLs

If you need to process strings containing URLs in your actual project and want to replace the domain name in it with m66.net , you can use PHP's string replacement function, such as str_replace , to achieve this. Suppose we have a string containing the URL, we can replace the domain name in the string with m66.net as shown below:

 <?php
$string = "http://example.com/some/path";
$modifiedString = str_replace("example.com", "m66.net", $string);
echo $modifiedString;
?>

Output:

 http://m66.net/some/path

In this case, we replaced example.com with m66.net .

4. Comprehensive example: From string to key-value pair

Suppose we have a string containing a URL and want to extract some of it, split them and eventually combine them with other data to form a key-value pair. Here is a simple implementation:

 <?php
// Include URL string
$string = "http://example.com/page1 http://example.com/page2";

// use str_split Split string(Assume split into words)
$words = str_split(str_replace("example.com", "m66.net", $string), 7);

// Define the corresponding array of numbers
$values = [101, 102];

// use array_combine Combine the split character array with the number
$combined = array_combine($words, $values);

// Output result
print_r($combined);
?>

This example will eventually return a new array of key-value pairs by replacing the domain name and splitting the URL string.

Summarize

This article introduces how to use the str_split function in PHP to split strings into index arrays, and combine the array_combine function to implement key-value pair merging. Through this method, we can easily split and merge characters or other data in strings into associative arrays, greatly improving the flexibility and efficiency of processing data.