V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ideaa
V2EX  ›  问与答

怎么把一个字符串数组,变为一个 PHP 可以识别的数组

  •  
  •   ideaa · Feb 6, 2015 · 3177 views
    This topic created in 4102 days ago, the information mentioned may be changed or developed.
    $a = "return array('a'=>1, 'b'=>2)";
    怎么把$a变为一个标准数组?不用eval
    Septembers
        1
    Septembers  
       Feb 6, 2015
    wozaimalu
        2
    wozaimalu  
       Feb 6, 2015
    $a = "return array('a'=>1, 'b'=>2)";
    preg_match_all("/\'([A-Za-z0-9])*\'/",$a,$key);
    preg_match_all("/=\>([0-9])*/",$a,$value);
    foreach($key[1] as $k => $v)
    $b[$v] = intval($value[1][$k]);
    var_dump($b);


    输出:
    array(2) {
    ["a"]=>
    int(1)
    ["b"]=>
    int(2)
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   812 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:54 · PVG 04:54 · LAX 13:54 · JFK 16:54
    ♥ Do have faith in what you're doing.