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

一个基础的PHP有关问题

WBOY
Release: 2016-06-13 13:06:12
Original
758 people have browsed it

一个基础的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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template