在PHP中,可以使用以下方法將字符串轉換為數組:
$str = "apple,banana,orange";
$arr = explode(",", $str);
print_r($arr);
輸出:
Array
(
[0] => apple
[1] => banana
[2] => orange
)
$str = "hello";
$arr = str_split($str);
print_r($arr);
輸出:
Array
(
[0] => h
[1] => e
[2] => l
[3] => l
[4] => o
)
$str = "apple,banana,orange";
$arr = preg_split("/,/", $str);
print_r($arr);
輸出:
Array
(
[0] => apple
[1] => banana
[2] => orange
)
$str = "Hello World";
$arr = str_word_count($str, 1);
print_r($arr);
輸出:
Array
(
[0] => Hello
[1] => World
)
這些是將字符串轉換為數組的一些常用方法,根據具體的需求和字符串的結構,選擇合適的方法進行轉換。