Home  >  Article  >  Backend Development  >  一个基础的PHP有关问题

一个基础的PHP有关问题

WBOY
WBOYOriginal
2016-06-13 13:06:12705browse

一个基础的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);

------解决方案--------------------

PHP code

         $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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn