php 将数组转化为字符串_PHP | 将字符串转换为字符数组

我就是我 2023-03-05 13:24 43阅读 0赞

php 将数组转化为字符串

Given a string and we have to convert it into a character array.

给定一个字符串,我们必须将其转换为字符数组。

Example:

例:

  1. Input:
  2. "WubbalubbaDubDub"
  3. Output:
  4. Array
  5. (
  6. [0] => W
  7. [1] => u
  8. [2] => b
  9. [3] => b
  10. [4] => a
  11. [5] => l
  12. [6] => u
  13. [7] => b
  14. [8] => b
  15. [9] => a
  16. [10] => D
  17. [11] => u
  18. [12] => b
  19. [13] => D
  20. [14] => u
  21. [15] => b
  22. )

PHP代码将字符串转换为字符数组 (PHP code to convert string to the character array)

  1. <?php
  2. //PHP code to convert string to the
  3. //character array
  4. //input string
  5. $input = "WubbalubbaDubDub";
  6. //converting string to character array
  7. //using str_split()
  8. $output = str_split($input);
  9. //printing the types
  10. echo "type of input : " .gettype($input) ."<br/>";
  11. echo "type of output: " .gettype($output) ."<br/>";
  12. //printing the result
  13. echo "input: " .$input ."<br/>";
  14. echo "output: " ."<br/>";
  15. print_r($output);
  16. ?>

Output

输出量

  1. type of input : string
  2. type of output: array
  3. input: WubbalubbaDubDub
  4. output:
  5. Array
  6. (
  7. [0] => W
  8. [1] => u
  9. [2] => b
  10. [3] => b
  11. [4] => a
  12. [5] => l
  13. [6] => u
  14. [7] => b
  15. [8] => b
  16. [9] => a
  17. [10] => D
  18. [11] => u
  19. [12] => b
  20. [13] => D
  21. [14] => u
  22. [15] => b
  23. )

Explanation:

说明:

We use the PHP str_split() function to split individual characters from a string into a character array. The string ($input) is split into individual characters and stored into ($output) and then printed as an array using the print_r() function.

我们使用PHP str_split()函数 将单个字符从字符串拆分为字符数组 。 字符串( $ input )被拆分为单个字符,并存储到( $ output )中,然后使用print_r()函数将其打印为数组。

翻译自: https://www.includehelp.com/php/convert-a-string-to-character-array.aspx

php 将数组转化为字符串

发表评论

表情:
评论列表 (有 0 条评论,43人围观)

还没有评论,来说两句吧...

相关阅读