0

我的 php 脚本中有这段代码

$concatenation= array('idcarde',$idcarde ,'mobile' ,$mobile ,'firstname',$firstname ,'lastname',$lastname ,'jour',$jour ,'mois',$mois ,'annee',$annee ,'adress',$adresse ,'Governora',$Governora);
$P = serialize( $concatenation );
$mabase = mysql_connect("localhost", "root", "root");
    if ($mabase) {
        mysql_select_db("mb", $mabase);
    }

    // insere toutes les donness provenant de $_POST
    $sql = " INSERT INTO `test` SET
            `formid` = '" . $formid . "',
            `data` = '" . mysql_escape_string($P) . "',
            `creation_date` = '" . date("Y-m-d H:i:s"). "' ";

    @mysql_query($sql, $mabase);

此代码将数据保存在我的表测试中

在列数据中我有这个结果:

a:18:{i:0;s:7:"idcarde";i:1;s:8:"00688009";i:2;s:6:"mobile";i:3;s:8:"52199200";i:4;s:9:"firstname";i:5;s:6:"dumas";i:6;s:8:"lastname";i:7;s:6:"alen";i:8;s:4:"jour";i:9;s:2:"05";i:10;s:4:"mois";i:11;s:2:"05";i:12;s:5:"annee";i:13;s:4:"1951";i:14;s:6:"adress";i:15;s:54:"11 rue paris";i:16;s:9:"Governora";i:17;s:5:"france";}

我想知道我们怎样才能消除这个值 i:0 i:1 i:2 i:3 ...

我也尝试使用此代码:

它以这种格式保存数据:

["idtype","CIN","mobile","20390112","idcard","00731429","firstname","samia","lastname","Mejri","email","","gender","M.","deliv_lieu","","deliv_d","","deliv_m","","deliv_y","","birthdate_d","08","birthdate_m","10","birthdate_y","1944","address","36 rue Grece ","city","France","gouv","France","profession","","field","","bank","","agency","","friends","","followers","","blog_url","","website","","civil_status","","nickname","","zipcode",""]

但是使用前面的代码它显示如下

a:18:{i:0;s:7:"idcarde";i:1;s:8:"00688009";i:2;s:6:"mobile";i:3;s:8:"52199200";i:4;s:9:"firstname";i:5;s:6:"dumas";i:6;s:8:"lastname";i:7;s:6:"alen";i:8;s:4:"jour";i:9;s:2:"05";i:10;s:4:"mois";i:11;s:2:"05";i:12;s:5:"annee";i:13;s:4:"1951";i:14;s:6:"adress";i:15;s:54:"11 rue paris";i:16;s:9:"Governora";i:17;s:5:"france";}

这就是我想要的,但我想消除 i:0 i:1 i:2 i:3 直到 i:17

我现在尝试使用此代码:

$concatenation= array('idcarde',$idcarde ,'mobile' ,$mobile ,'firstname',$firstname ,'lastname',$lastname ,'jour',$jour ,'mois',$mois ,'annee',$annee ,'adress',$adresse ,'Governora',$Governora);
$P = serialize( $concatenation );
$M=preg_replace('~i:[0-9];~', '', $P);
$mabase = mysql_connect("localhost", "root", "root");
    if ($mabase) {
        mysql_select_db("mb", $mabase);
    }

    // insere toutes les donness provenant de $_POST
    $sql = " INSERT INTO `test` SET
            `formid` = '" . $formid . "',
            `data` = '" . mysql_escape_string($M) . "',
            `creation_date` = '" . date("Y-m-d H:i:s"). "' ";

    @mysql_query($sql, $mabase);

此代码从数据 i:1 中消除;我:2;直到我:9;但不能消除

例如我:10;我:11;我:12;

我尝试解决这个问题但没有成功

$M=preg_replace('~i:~;~', '', $P);

我尝试将a :18更改为a:28

$P = serialize($concatenation) ;
  //$M=preg_replace('~i:[0-9];~', '', $P);
 $M=preg_replace('~i:[0-9]+;~', '', $P);
  $M1=preg_replace('a:[0-9]+~', 'a:28', $M); 

但它显示错误

我找到解决方案使用 $M1=preg_replace('~a:[0-9]+~', 'a:28', $M);

但我想说 28 是 $P 中的元素数

所以我应该在 $P 中找到元素的数量,然后在 a

4

3 回答 3

1

在 PHP 中,serialize不序列化为 JSON。如果你想要 JSON,你应该json_encode改用:

$P = json_encode($concatenation);

编辑:这个正则表达式将从i:<digits>序列化数据中删除,但请记住,一旦删除它,您就不unserialize能再生成结果字符串,所以我不确定您为什么要这样做:

$M = preg_replace('/([{;])i:\d+;/', '$1', $P);
于 2013-02-13T13:39:18.043 回答
0

serialize()函数与json_encode 不同,它用于保存变量名称及其值,稍后您可以使用universalize()则一切都可用。为此,请使用json_encode()json_decode()

于 2013-02-13T13:40:10.893 回答
0

因此,a:number那里表示您有一个数组以及其中的元素数量这一事实。所有这些i:number;都是数组的键。它们一直上升到您拥有的元素数量 - 1(因为它以 0 开头)

如果您删除密钥,它将像反序列化一样

 ['idcarde'] => '00688009'
 ['mobile'] => '5265656'

如果这是你想要的,那么在第一行

$concatenation= array('idcarde',$idcarde ,'mobile' ,$mobile ,'firstname',$firstname ,'lastname',$lastname ,'jour',$jour ,'mois',$mois ,'annee',$annee ,'adress',$adresse ,'Governora',$Governora);

只需用 a 替换所有其他逗号=>即可

于 2013-02-13T15:29:38.573 回答