drgbpq5930 2013-10-09 09:50
浏览 53
已采纳

我想将字符串(关联数组字符串)转换为关联数组?

i want to store associative array into a variable a as a string, and then convert the variable into array.

$var='"electirc_bill"=>array(
                                   "type" => "number",
                                    "required"=>"yes"
                                  ),
      "electirc_bill_per"=>array(
                                   "type" => "number",
                                    "required"=>"yes"
                                 ),                   
              "gass_bill"=>array(     
                                   "type" => "number",
                                    "required"=>"yes"
                                  )
    )';

    var_dump($var);
  • 写回答

5条回答 默认 最新

  • dqask02082 2013-10-09 09:53
    关注

    Use serialize and unserialize.

    Convert the array to string:

    $string = serialize($array);
    

    Convert it back to an array:

    $array = unserialize($string);
    

    Edit: Based on your comment you seem to already have the array stored as a string and want to be able to convert it to an array. For that I would use eval but be cautious when using it with any user input as it could lead to security vulnerabilities within your code.

    I've made a small example using your code here: http://codepad.org/rPNXPBlW

    $var = '$array_var = array("One" => array("1.1", "1.2"), "Two" => array("2.1", "‌​2.2"));';
    
    eval($var);
    
    echo $array_var['One'][0]; // Shows 1.1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?