给定一个字符串,我们必须将其转换为字符数组。
示例
Input: "WubbalubbaDubDub" Output: Array ( [0] => W [1] => u [2] => b [3] => b [4] => a [5] => l [6] => u [7] => b [8] => b [9] => a [10] => D [11] => u [12] => b [13] => D [14] => u [15] => b )
<?php // PHP代码将字符串转换为 //字符数组 //输入字符串 $input = "WubbalubbaDubDub"; //将字符串转换为字符数组 //使用str_split() $output = str_split($input); //打印类型 echo "type of input : " .gettype($input) ."<br/>"; echo "type of output: " .gettype($output) ."<br/>"; //打印结果 echo "input: " .$input ."<br/>"; echo "output: " ."<br/>"; print_r($output); ?>
输出结果
type of input : string type of output: array input: WubbalubbaDubDub output: Array ( [0] => W [1] => u [2] => b [3] => b [4] => a [5] => l [6] => u [7] => b [8] => b [9] => a [10] => D [11] => u [12] => b [13] => D [14] => u [15] => b )
说明:
我们使用PHP str_split()函数,以单个字符从一个字符串分割成字符数组。字符串($input)被拆分为单个字符,并存储到($output)中,然后使用print_r()函数以数组形式打印。