Php saves the database program code to the array. When caching files, we often need to convert php code or arrays into strings and save them to the database. below I will introduce two methods to save arrays to the database. Method 1: When using the cache file, we often need to convert the php code or array into a string and save it to the database. below I will introduce two methods to save the array to the database.
Method 1:
Write with serialize, and then output with unserialize
Serialize () is to serialize the values of PHP variables such as objects and arrays into strings and store them. serialized strings can be stored elsewhere, such as databases, sessions, and cookies. serialized operations do not lose the type and structure of these values. In this way, the data of these variables can be transmitted on the PHP page, or even between different PHP programs.
Unserialize () is to convert the serialized string back to the PHP value. Returns the converted value, which can be integer, float, string, array, or object. if the passed string cannot be deserialized, FALSE is returned.
| The code is as follows: |
|
Class db { Private $ host; Private $ user; Private $ pwd; Private $ dbname; Private $ Mysqli; Function _ construct ($ host, $ user, $ pwd, $ dbname ){ $ This-> host = $ host; $ This-> user = $ user; $ This-> pwd = $ pwd; $ This-> dbname = $ dbname; $ This-> db (); } Function db (){ $ This-> mysqli = new mysqli ($ this-> host, $ this-> user, $ this-> pwd, $ this-> dbname ); } Function select (){ $ This-> mysqli-> query ("set charset gbk "); $ SQL = "SELECT id, cname FROM hdw_channel "; $ Result = $ this-> mysqli -> Query ($ SQL ); $ Rows = array (); While ($ row = $ result-> fetch_assoc ()){ $ Rows [] = $ row; } ECHO" "; Print_r ($ rows ); } Function _ wakeup () {// deserialization, $ This-> db (); } } $ Chanel = new db ("localhost", 'root', '', 'hdcm '); // $ Chanel-> select (); Session_start (); $ _ SESSION ['Channel _ obj '] = serialize ($ chanel); // serializes an object and stores its attributes. no method is available. Therefore, use _ wakeup () Class ren { Private $ name; Private $ age; Function _ construct ($ name, $ age ){ $ This-> name = $ name; $ This-> age = $ age; } Function show (){ Echo "name: {$ this-> name} age: {$ this-> age }"; } Function _ sleep (){ Return array_keys (get_object_vars ($ this); // or get the key name in the array to serialize some variables. } } $ Zao = new ren ("Zhao Liu", 44 ); Echo serialize ($ zao); // serialization (specify the variable to be serialized)
========================================== Session_start (); Include '59. php '; $ Channel_obj = unserialize ($ _ SESSION ['Channel _ obj ']); // deserialization class object $ Channel_obj-> select (); // The _ wakeup method can be used. |
Method 2:
Write with json_encode and output with json_decode
Before json_encode, use urlencode () to process all the content in the array, convert json_encode () to a json string, and then use urldecode () to convert the encoded Chinese character back.
| The code is as follows: |
|
/*************************************** *********************** * * Use a specific function to process all elements in the array * @ Param string & $ array the string to be processed * @ Param string $ function the function to be executed * @ Return boolean $ apply_to_keys_also whether it is also applied to the key * @ Access public * **************************************** *********************/ Function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false) { Static $ recursive_counter = 0; If (++ $ recursive_counter> 1000 ){ Die ('possible deep recursion attack '); } Foreach ($ array as $ key => $ value ){ If (is_array ($ value )){ ArrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also ); } Else { $ Array [$ key] = $ function ($ value ); } If ($ apply_to_keys_also & is_string ($ key )){ $ New_key = $ function ($ key ); If ($ new_key! = $ Key ){ $ Array [$ new_key] = $ array [$ key]; Unset ($ array [$ key]); } } } $ Recursive_counter --; } /*************************************** *********************** * * Convert an array to a JSON string (compatible with Chinese characters) * @ Param array $ array the array to be converted * @ Return string the converted json string * @ Access public * **************************************** *********************/ Function JSON ($ array ){ ArrayRecursive ($ array, 'urlencode', true ); $ Json = json_encode ($ array ); Return urldecode ($ json ); } $ Array = array ( 'Name' => 'chia ', 'Age' => 20 ); Echo JSON ($ array ); ?>
|
Bytes. Method 1: use...