PHP:从带数字的字符串到数组

时间:2013-07-03 09:39:49

标签: php arrays

我有一个包含这样数字的字符串:

$string = "694,852,94,94,743,202,127,127";

在PHP中从此字符串中获取整数数组的最佳和最快方法是什么?

Array ( [0] => 694,
        [1] => 852,
        [2] => 94,
        [3] => 94,
        [4] => 743,
        [5] => 202,
        [6] => 127,
        [7] => 127 )

9 个答案:

答案 0 :(得分:4)

$string = "694,852,94,94,743,202,127,127";

$array = explode(',',$string);

PHP Official Documentation

答案 1 :(得分:2)

$string = "694,852,94,94,743,202,127,127";
$array = explode(",", $string);

答案 2 :(得分:2)

试试这个

$string = "694,852,94,94,743,202,127,127";

$num  = explode(",", $string);

$num将包含从字符串中拆分的数字数组。

请参阅PHP Documentation。请先询问一次,然后再询问此处。

答案 3 :(得分:2)

$string = "694,852,94,94,743,202,127,127";
$arr = explode(",",$string);

如果您尝试在计算中使用数字,Php将自动进行类型转换。 How do I convert a string to a number in PHP?

答案 4 :(得分:1)

您应该使用explode来构建您期望的数组:

$arr = explode(',',$string);

答案 5 :(得分:1)

您可以使用explode使用分隔符将字符串转换为数组。例如

 $array = explode(",", $string);

答案 6 :(得分:1)

<?php
    $string = "694,852,94,94,743,202,127,127";
    $arr = explode(",", $string);

答案 7 :(得分:1)

使用explode()

$string = "694,852,94,94,743,202,127,127";

$array = explode(",",$string);

答案 8 :(得分:0)

尝试explode()喜欢

$string = "694,852,94,94,743,202,127,127";
$my_array = explode("," , $string); 
print_r($my_array);

请参阅此explode