我有一個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