一个基础的PHP问题
问题:
将字符串赋值给数组。
字符串:$str = 1,2,3,4,0,34,0,8,55,0;
数组:$array = array(1,2,3,4,0,34,0,8,55,0 );
我想用变量做为参数传值给数组,请问要如何写?
下面的写法是错误的:
$array = array($str);
------解决方案--------------------
$str="1,2,3,4,0,34,0,8,55,0"; $arr = array(); $strArr = explode(",",$str); foreach($strArr as $k=>$v){ $arr[] = $v; } var_dump($arr); <div class="clear"> </div>