I have a multidimensional array in PHP like this:
我在PHP中有一个多维数组,如下所示:
$array = array(
"Part1" => array(
"Subpart1" => array(0, 1),
"Subpart2" => array(1, 0)
),
"Part2" => array(0),
"Part3" => array(0, 1, 0)
);
Now I want to store this array in a MySQL table and retrieve it exactly like this again on another PHP page.
现在我想将这个数组存储在一个MySQL表中,并在另一个PHP页面上再次检索它。
I've been trying using serialize()
and unserialize()
我一直在尝试使用serialize()和unserialize()
$array= serialize($array);
and then on the other page
然后在另一页上
$array= $row['Array'];
$array2 = array();
$array2 = unserialize($array);
But I seem to do something wrong, in the beginning I got a var_dump
of bool(false) and now I get a var_dump
of NULL.
但是我似乎做错了什么,一开始我得到了一个bool(false)的var_dump,现在我得到一个NULL的var_dump。
4 个解决方案
#1
8
Your code looks ok...
你的代码看起来不错......
One thing that can catch you out is if your column is too small - if you use VARCHAR(255) your data can be truncated and won't unserialize. If you add the value of $row['Array']
I could see if it's whole.
有一件事可以解决你的问题,如果你的列太小 - 如果你使用VARCHAR(255),你的数据可能会被截断而不会反序列化。如果你添加$ row ['Array']的值,我可以看看它是否完整。
#2
3
Use column type TEXT. Serialized data often doesn't fit VARCHAR(255).
使用列类型TEXT。序列化数据通常不适合VARCHAR(255)。
#3
1
You could use json encode, json_encode($array) and you will get a string value in json notation so you can store in database, and retrive and do a json_decode($string, true) so you cand convert in an array again. If you don't pass the true argument to the json_decode, it will converted to a stdClass.
你可以使用json编码,json_encode($ array),你将得到一个json表示法的字符串值,这样你就可以存储在数据库中,并且可以执行json_decode($ string,true),这样你就可以再次转换为数组。如果没有将true参数传递给json_decode,它将转换为stdClass。
#4
0
Particulars Quantity Rate Amount Amount Sum Others Others Amount Tax TotalThis is my table in php/html. I need to store and retrieve the php Particulars(p),Quantity(q),Rate(r) as a separate table format
这是我在php / html中的表格。我需要存储和检索php Particulars(p),Quantity(q),Rate(r)作为单独的表格格式
#1
8
Your code looks ok...
你的代码看起来不错......
One thing that can catch you out is if your column is too small - if you use VARCHAR(255) your data can be truncated and won't unserialize. If you add the value of $row['Array']
I could see if it's whole.
有一件事可以解决你的问题,如果你的列太小 - 如果你使用VARCHAR(255),你的数据可能会被截断而不会反序列化。如果你添加$ row ['Array']的值,我可以看看它是否完整。
#2
3
Use column type TEXT. Serialized data often doesn't fit VARCHAR(255).
使用列类型TEXT。序列化数据通常不适合VARCHAR(255)。
#3
1
You could use json encode, json_encode($array) and you will get a string value in json notation so you can store in database, and retrive and do a json_decode($string, true) so you cand convert in an array again. If you don't pass the true argument to the json_decode, it will converted to a stdClass.
你可以使用json编码,json_encode($ array),你将得到一个json表示法的字符串值,这样你就可以存储在数据库中,并且可以执行json_decode($ string,true),这样你就可以再次转换为数组。如果没有将true参数传递给json_decode,它将转换为stdClass。
#4
0
Particulars Quantity Rate Amount Amount Sum Others Others Amount Tax TotalThis is my table in php/html. I need to store and retrieve the php Particulars(p),Quantity(q),Rate(r) as a separate table format
这是我在php / html中的表格。我需要存储和检索php Particulars(p),Quantity(q),Rate(r)作为单独的表格格式