Hi there, can you please help me? i have data in array and i want to insert it in a single table. structure of table and array to be store is shown below: Please let me know how to store this array in database? Database Structure: array-structure database-Browse Let me know any solution for this? Thanks in anticipation
I made 2 crazy functions for you. They can get array/dbdata and transform it dbdata/array. dbdata is a string I named, which uses as an array. You can use this to hold all of your data in varchar/text field/s in the database. You can make either one field for each cell in the first level of the array, or just take everything into one field. function to_dbdata($var,$varname='dbdata',$level=1) { $new_string = $varname.chr(1); if(is_array($var)) { $new_string .= chr($level+2); foreach($var as $key=>$value) { $new_string .= to_dbdata($value,$key,$level+1); } $new_string .= chr($level+3); } elseif(is_string($var)) $new_string .= $var; $new_string .= chr(2); return $new_string; } function from_dbdata($dbdata,$level=1) { preg_match_all('#([a-zA-Z0-9]+)'.chr(1).chr($level+2).'(.*)'.chr($level+3).chr(2).'#Us',$dbdata,$match1); for($i=0;$i<count($match1[1]);$i++) { $dbdata = str_replace($match1[0][$i],'',$dbdata); $new_data[$match1[1][$i]] = from_dbdata($match1[2][$i],$level+1); } preg_match_all('#([a-zA-Z0-9]+)'.chr(1).chr($level+2).'{0}(.*)'.chr($level+3).'{0}'.chr(2).'#Us',$dbdata,$match2); for($i=0;$i<count($match2[1]);$i++) { $new_data[$match2[1][$i]] = $match2[2][$i]; } return $new_data; } PHP: Usage example: Input: $cool_array = array("hey"=>"there",array("yes","no"=>"sir"),"what is up"); print_r(from_dbdata(to_dbdata($cool_array,'cool_array'))); Output: Array ( [cool_array] => Array ( [0] => Array ( [0] => yes [no] => sir ) [hey] => there [1] => what is up ) ) PHP:
hey thanks buddy for your reply. But i have a better function than you have suggested. What my question was , i need a sample query or codeso that i can get idea to store above array in database. Can you suggest me something else?
Thanks for your quick reply. Here is explanation: You can see above database screen-shots that i have posted recently. It has a column temp_property_name. I am getting data in multidimensional array and need to store both ([0] & [1]) value of temp_property_name in a single column without using different table for each. Same for other like _add , _price etc Can you please let me know any sample query or code to achieve this in a single table?
Thanks for your quick reply. Here is explanation: You can see above database screen-shots that i have posted recently. It has a column temp_property_name. I am getting data in multidimensional array and need to store both ([0] & [1]) value of temp_property_name in a single column without using different table for each. Same for other like _add , _price etc Can you please let me know any sample query or code to achieve this in a single table?
Just use the implode + explode functions. http://il2.php.net/manual/en/function.implode.php http://il2.php.net/manual/en/function.explode.php
Quickest way possible: $array = array( "key" => "value" ); // etc etc... $string = serialize($array); PHP: This turns $array into a string that looks like: a:1:{s:3:"key";s:5:"value";} PHP: This string can be saved as regular text (varchar, longtext, etc). To get your array back: $array = unserialize($string); print_r($array); PHP: This only really works for simple arrays, though. Objects and other such nasty types, you'll have problems with those. Also, I've heard that it's a good idea to base64_encode() before storing and base64_decode() before unserialize()ing - sometimes MySQL does funny things with empty elements.