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 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟