我有一个5级多维数组。数组中的键数量会波动,但我需要将其存储在数据库中,以便稍后使用PHP访问它。有没有简单的方法来做到这一点?将数组存储在MySQL表中
我的想法是使用几个不同的分隔符(如#*
和%*
)将数组转换为单个字符串,然后在需要时使用一系列explode()将数据转换回数组。
我现在还没有写任何代码,因为我希望有更好的方法来做到这一点。但我有我试过下面勾勒出一个潜在的解决方案:
这里是我的阵列的概述:
n=button number
i=item number
btn[n][0] = button name
btn[n][1] = button desc
btn[n][2] = success or not (Y or N)
btn[n][3] = array containing item info
btn[n][3][i][0] = item intput type (Default/Preset/UserTxt/UserDD)
btn[n][3][i][1] = array containing item value - if more than one index then display as drop down
这是我打算用分隔符的破败:
#*Button Title //button title
&*val1=*usr1234 //items and values
&*val2=*FROM_USER(_TEXT_$*name:) //if an items value contains "FROM_USER" then extract the data between the perenthesis
&*val3=*FROM_USER(_TEXT_$*Time:) //if the datatype contains _TEXT_ then explode AGAIN by $* and just display a textfield with the title
&*val4=*FROM_USER($*[email protected]*value1$*[email protected]*value2) //else explode AGAIN by $* for a list of name value pairs which represent a drop box - [email protected]*value2
//sample string - a single button
#*Button Title%*val1=*usr1234&*val2=*FROM_USER(_TEXT_$*name:)&*val3=*FROM_USER(_TEXT_$*date:)&*val4=*FROM_USER($*[email protected]*value1$*[email protected]*value2)
我希望这是有道理的。无论哪种方式。如果任何人都可以给我一些关于如何在单个数据库表中存储多维数组的想法,我会非常感激。
你可以在单个表中使用子父关系想要在行级别存储或者你可以将你的数组编码到json中。 – Neo 2013-03-28 09:31:37
你可以json_encode并保存该字符串。 – Ryoku 2013-03-28 09:32:08
你可以使用PHP的序列化/反序列化将数组转换为/从字符串 – 2013-03-28 09:33:03