比序列化反序列化快的var_export 存入DB后反回位数组的方法

jackxiang 2010-8-11 10:52 | |
版权归膘哥,来源:
膘叔(19129540)  10:45:16

$a = array (1, 2, array ("a", "b", "c"));
$b = var_export ($a,TRUE);
eval( "\$c=\$b;" );
echo '<pre>';
print_r( $c );
echo '</pre>';
膘叔(19129540)  10:45:35
var_export后是存成字符串了。

接合swoole实践:


输出:
curl http://127.0.0.1:9501/abc
    Swoole\Http\Request::__set_state(array(
   'fd' => 1,
   'header' =>
  array (
    'user-agent' => 'curl/7.29.0',
    'host' => '127.0.0.1:9501',
    'accept' => '*/*',
  ),
   'server' =>
  array (
    'request_method' => 'GET',
    'request_uri' => '/abc',
    'path_info' => '/abc',
    'request_time' => 1649747144,
    'request_time_float' => 1649747144.16464,
    'server_protocol' => 'HTTP/1.1',
    'server_port' => 9501,
    'remote_port' => 62006,
    'remote_addr' => '127.0.0.1',
    'master_time' => 1649747143,
  ),
   'cookie' => NULL,
   'get' => NULL,
   'files' => NULL,
   'post' => NULL,
   'tmpfiles' => NULL,
))
    Hello, world!


回忆未来(372647693)  10:58:07

<?php
$a = array (1, 2, array ("a", "b", "c"));
$b = var_export ($a,TRUE);
eval( "\$c=\$b;" );
var_dump($c);



string(94) "array (
  0 => 1,
  1 => 2,
  2 =>
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)"
是个string啊。。。

最后,═ 云下遮雨  11:28:47 给出了个解决方案:
eval( '$c='.$b.';');
eval( "\$c={$b};" );
eval( "\$c=".$b.";" );



我尝试了一下,确实还回位原来的数组了,代码如下:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<?php
$a = array (1, 2, array ("a", "b", "c"));
$b = var_export ($a,TRUE);
//eval( "\$c=".$b.";" );
eval( "\$c={$b};" );
print_r($c);
//print_r( $c );

?>








+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


[~]# php var_export.php

Array
(
    [0] => 1
    [1] => 2
    [2] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)


指出了膘哥的一个小错误:
═ 云下遮雨  11:38:46
eval( "\$c=\$b" ); 这样就是错的,相当于 $c=$b

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/3384/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2022-4-12 15:07
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]