数组转换为字符串并把它返回到PHP数组 [英] convert array to string and get it back to array in PHP

查看:212
本文介绍了数组转换为字符串并把它返回到PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的序列化功能来保存在我的MySQL数据库的数组,然后我反序列化他在其他页面。
该阵列结构是这样的:

I'm using the Serialize function to store an array in my MYSQL database, and then I'm unSerialize Him in other page. The array structure look like this :

Array ( [0] => Array ( [names] => somename1 [rating] => 10 ) [1] => Array ( [names] => somename2 [rating] => 9 ) )

当我插入数组到数据库中我使用这个功能,将其转换为字符串:

When I INSERT the array to the database I'm using this function to convert it to string :

$array_string=mysql_escape_string(serialize($arr));

然后,当我做了反序列化,我不知道如何恢复字符串(数组中的数据库)的确切结构,这是以前。 (如何convers这个字符串返回数组)

我知道我必须使用此行:

I know I have to use this line :

$arr=explode("|",$list);

在某种程度上,但我不能将其恢复到阵列是之前的确切结构。
此行的结果是在阵列的结构有一点不同:

In some way, but I can't restore it to the exactly structure of the array it was before. The result of this line is a little bit different in the structure of the array :

Array ( [0] => Array( [0] => Array ( [names] => d [rating] => 7 ) [1] => Array ( [names] => b [rating] => 6 )  ) )

感谢

推荐答案

尝试 json_en code json_de code

$array_to_store = array(....);
$str_array = json_encode($array_to_store);
//Store $str_array
//Retrieve it and to make it an array again
$array = json_decode($str_array, true);

******************* Edited *********************

我看不出有什么不对序列化和反序列化:

I do not see what is wrong with serialize and unserialize:

$array = array(
        array(
                'names' => 'somename1',
                'rating' => 10
             ),
        array(
                'names' => 'somename2',
                'rating' => 9
             )
    );

//Array before
print_r('<pre>');
print_r($array);

//Serialised Array
$s = serialize($array);

print_r('<pre>');
print_r($s);

//Unserialised Array
print_r('<pre>');
print_r(unserialize($s));

这篇关于数组转换为字符串并把它返回到PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆