如何在php中将字符串转换为数组

时间:2021-01-09 21:18:51

I am fetching records from mysql database through while loop.

我正在通过while循环从mysql数据库获取记录。

$sql=mysql_query("SELECT * FROM animate ORDER BY RAND() LIMIT 10") or die("query Field");

 while($row=mysql_fetch_array($sql)){

$row['code'];

 }

output is xyacefg.

xyacefg输出。

Now I want to break this output into an array I want to place each letter into separate index of array like

现在我想把这个输出分解成一个数组,我想把每个字母放到数组的单独索引中

array('x','y','a','c','e','f','g');

阵列(‘x’,‘y’,‘‘,‘c’,‘e’,‘f’,‘g’);

I have used explode

我用爆炸

$array = explode(' ', $row['code']);

$array =爆炸式(',$row['code']]);

but it didnot work. now the final code is .

但它没有工作。现在,最后的代码是。

$sql=mysql_query("SELECT * FROM animate ORDER BY RAND() LIMIT 10") or die("query Field");

 while($row=mysql_fetch_array($sql)){

$row['code'];

 }

$array = explode(' ', $row['code']);

3 个解决方案

#1


2  

You need to create an empty array and assign value to them like below:-

您需要创建一个空数组,并为其赋值如下:-

$new_array = array(); // create a new array
$sql=mysql_query("SELECT * FROM animate ORDER BY RAND() LIMIT 10") or die("query Field");

 while($row=mysql_fetch_array($sql)){

    $new_array[] = $row['code']; // assign value to array

 }
echo "<pre/>";print_r($new_array); // print array which is created

Note:- My assumption is $row['code'] giving you value one-by-one because it is in while loop.

注意:-我的假设是$row['code']给你一个一个的值,因为它是在while循环中。

#2


2  

Its easy with str_split:

其简单的函数:

<?php
$array = str_split("xyacefg"); //In your case: str_split($row['code']);
print_r($array);

Output:

输出:

Array
(
    [0] => x
    [1] => y
    [2] => a
    [3] => c
    [4] => e
    [5] => f
    [6] => g
)

#3


0  

Just use str_split() function. If you want to get exact same result as you want like:

用函数()函数。如果你想要得到和你想要的完全一样的结果:

array('x','y','a','c','e','f','g');

then use following code:

然后使用以下代码:

<?php
  $string = "xyacefg";
  $exploded = str_split($string);
  echo 'array("'.implode('", "', $exploded).'");';
?>

#1


2  

You need to create an empty array and assign value to them like below:-

您需要创建一个空数组,并为其赋值如下:-

$new_array = array(); // create a new array
$sql=mysql_query("SELECT * FROM animate ORDER BY RAND() LIMIT 10") or die("query Field");

 while($row=mysql_fetch_array($sql)){

    $new_array[] = $row['code']; // assign value to array

 }
echo "<pre/>";print_r($new_array); // print array which is created

Note:- My assumption is $row['code'] giving you value one-by-one because it is in while loop.

注意:-我的假设是$row['code']给你一个一个的值,因为它是在while循环中。

#2


2  

Its easy with str_split:

其简单的函数:

<?php
$array = str_split("xyacefg"); //In your case: str_split($row['code']);
print_r($array);

Output:

输出:

Array
(
    [0] => x
    [1] => y
    [2] => a
    [3] => c
    [4] => e
    [5] => f
    [6] => g
)

#3


0  

Just use str_split() function. If you want to get exact same result as you want like:

用函数()函数。如果你想要得到和你想要的完全一样的结果:

array('x','y','a','c','e','f','g');

then use following code:

然后使用以下代码:

<?php
  $string = "xyacefg";
  $exploded = str_split($string);
  echo 'array("'.implode('", "', $exploded).'");';
?>