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条)

报告相同问题?

悬赏问题

  • ¥15 openwrt双栈NAT
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用