字符串为array('name'=>'tom','pw'=>'hello','email'=>'[email protected]')
如何转为数组array('name'=>'tom','pw'=>'hello','email'=>'[email protected]'),
只改变类型(用php,要有较高转换效率),求解,谢谢

解决方案 »

  1.   

    "arr.php" 5L, 121C written                                                                                                                                     
    [User:root Time:08:49:42 Path:/home/liangdong/php]$ php arr.php 
    Array
    (
        [name] => tom
        [pw] => hello
        [email] => [email protected]
    )
    [User:root Time:08:49:42 Path:/home/liangdong/php]$ cat arr.php 
    <?php
    $str = "array('name'=>'tom','pw'=>'hello','email'=>'[email protected]');";
    eval('$arr = ' . $str);
    print_r($arr);
    ?>
      

  2.   

    $s="array('name'=>'tom','pw'=>'hello','email'=>'[email protected]')";
    $arr=eval("return $s;");
    var_dump($arr);
      

  3.   


    学习了,刚才还在看:
    eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned. If there is a parse error in the evaluated code, eval() returns FALSE and execution of the following code continues normally. It is not possible to catch a parse error in eval() using set_error_handler().这句话的意思,这下了了。
      

  4.   


    输出结果是:array(3) { ["name"]=>  string(3) "tom" ["pw"]=>  string(5) "hello" ["email"]=>  string(15) "[email protected]" } 
    怎么把string(3) string(5) string(15)这些信息去掉啊?
      

  5.   

    能不能把结果变为数组array('name'=>'tom','pw'=>'hello','email'=>'[email protected]'),麻烦再帮忙改一下~~谢了~~
      

  6.   

    那就是数组了。你用print_r($arr);  看看吧。
      

  7.   

    哦,是生成数组了,但我要用它作为查询mongodb的条件,格式要为array('name'=>'tom','pw'=>'hello','email'=>'[email protected]'),当然使用时只用array('name'=>'tom'),能不能把格式转一下~~
      

  8.   

    本人刚接触php,很不熟悉它的用法,别见笑
      

  9.   

    使用时只能用array('name'=>'tom'),用Array ( [name] => tom )得不到访问结果,改不了数据库操作的类,要改一下格式 ~~
      

  10.   

    我知道。你传递时。把$arr直接传递行就行了。比如:
    foo($arr);
    function foo($ar=array()){
         .........................
    }