dongsha9208 2014-02-18 09:55
浏览 90
已采纳

将字符串内容放入数组php中

I have this string

string(2091) "


"roots" => array(
    array(
        "driver"        => "LocalFileSystem",   // driver for accessing file system (REQUIRED)
        "path"          => "../files/",         // path to files (REQUIRED)
        //"URL"           => dirname($_SERVER["PHP_SELF"]) . "/../files/", // URL to files (REQUIRED)
        "accessControl" => "access" ,            // disable and hide dot starting files (OPTIONAL)
        "alias" => "Root",
        //"uploadDeny"    => array("all"),
        "attributes" => array(
    array(
        "pattern" => "/\manuali$/",  //You can also set permissions for file types by adding, for example, <b>.jpg</b> inside pattern.
       // "pattern" =>"/\PROVA$/",
        "read" => true,
        "write" => false,
        "locked" => true,
    ),
    array(
        "pattern" => "/rapporti$/",  //You can also set permissions for file types by adding, for example, <b>.jpg</b> inside pattern.
       // "pattern" =>"/\PROVA$/",
        "read" => true,
        "write" => true,
        "locked" => true,
    )
     [...]
     ";

i want to put into array the entire value of a string. Ex:

array(


"roots" => array(
    array(
        "driver"        => "LocalFileSystem",   // driver for accessing file system (REQUIRED)
        "path"          => "../files/",         // path to files (REQUIRED)
        //"URL"           => dirname($_SERVER["PHP_SELF"]) . "/../files/", // URL to files (REQUIRED)
        "accessControl" => "access" ,            // disable and hide dot starting files (OPTIONAL)
        "alias" => "Root",
        //"uploadDeny"    => array("all"),
        "attributes" => array(
    array(
        "pattern" => "/\manuali$/",  //You can also set permissions for file types by adding, for example, <b>.jpg</b> inside pattern.
       // "pattern" =>"/\PROVA$/",
        "read" => true,
        "write" => false,
        "locked" => true,
    ),
    array(
        "pattern" => "/rapporti$/",  //You can also set permissions for file types by adding, for example, <b>.jpg</b> inside pattern.
       // "pattern" =>"/\PROVA$/",
        "read" => true,
        "write" => true,
        "locked" => true,
    )
     [...]
     );

i have tried str_split() implode() array_push()... but every function put the string into array in this mode array(string(" "))i want to put in this mode array(" ").

Thanks

  • 写回答

2条回答 默认 最新

  • dsf5989 2014-02-18 10:09
    关注

    If your string is parseable and you don't mind about possible injections, you can simply eval your array representation.

    For example, if your string is in $arrayStr and you want to create an array $myArray from that:

    $arrayStr = '"roots" => array ( ... )';
    eval('$myArray = array(' . $arrayStr . ');');
    

    Note that if $arrayStr is not entirely controller by you from generation to conversion, it is highly risky since anything in it would be evaluated.

    A better solution to exchange a whole array between parts of your application would be to serialize() it beforehand to create a string representation of your array/object which can be stored, and then unserialize it when you need it in its original form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致