如何将字符串转换为数组

时间:2016-12-29 10:39:47

标签: php arrays string

如何转换字符串ID

$ids = '8552479333,8552438544,';

看起来像一个数组

[" 8552479333"" 8552438544"]

我试过:

$ids = (explode(",",$ids,-1));

但是这给了我:

Array ( [0] => 8552479333 [1] => 8552438544)

2 个答案:

答案 0 :(得分:1)

这是一种非常具体的格式化方式。您可能会错过使用json编码例程来获得您正在寻找的确切结果:

<?php
$ids = '8552479333,8552438544,';
$string = json_encode(explode(',', $ids, -1));
var_dump($string);

输出显然是:

string(27) "["8552479333","8552438544"]"

答案 1 :(得分:0)

<?php
$ids = '8552479333,8552438544,';
$ids = json_encode(explode(',', $ids, -1));
print_r($ids);
?>