我有一个 5 级多维数组。数组中键的数量会波动,但我需要将其存储在数据库中,以便以后可以使用 PHP 访问它。有什么简单的方法可以做到这一点吗?
我的想法是使用多个不同的分隔符(如 #*
和 %*
)将数组转换为单个字符串,然后使用一系列 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($*name1@*value1$*name2@*value2) //else explode AGAIN by $* for a list of name value pairs which represent a drop box - name2@*value2
//sample string - a single button
#*Button Title%*val1=*usr1234&*val2=*FROM_USER(_TEXT_$*name:)&*val3=*FROM_USER(_TEXT_$*date:)&*val4=*FROM_USER($*name1@*value1$*name2@*value2)
总而言之,我正在寻找有关如何在单个数据库表中存储多维数组的一些想法。
最佳答案
你想要的是一个data serialization方法。不要发明你自己的,那里已经有很多了。最明显的候选者是 JSON ( json_encode
) 或 PHP 特定的 serialize
. XML 也是一种选择,尤其是当您的数据库可能在某种程度上原生支持它时。
关于php - 将数组存储在 MySQL 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15678263/