How to store multidimensional array in database?

Discussion in 'PHP' started by rahulephp, Dec 8, 2009.

  1. #1
    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:
    [​IMG]

    array-structure
    [​IMG]

    database-Browse
    [​IMG]

    Let me know any solution for this?
    Thanks in anticipation
     
    rahulephp, Dec 8, 2009 IP
  2. The Chosen Generl

    The Chosen Generl Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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:
     
    The Chosen Generl, Dec 8, 2009 IP
  3. rahulephp

    rahulephp Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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?
     
    rahulephp, Dec 8, 2009 IP
  4. The Chosen Generl

    The Chosen Generl Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you have a function similar to this I don't understand what is the problem.
    Please explain =]
     
    The Chosen Generl, Dec 8, 2009 IP
  5. rahulephp

    rahulephp Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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?
     
    rahulephp, Dec 8, 2009 IP
  6. rahulephp

    rahulephp Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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?
     
    rahulephp, Dec 8, 2009 IP
  7. The Chosen Generl

    The Chosen Generl Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Just use the implode + explode functions.
    http://il2.php.net/manual/en/function.implode.php
    http://il2.php.net/manual/en/function.explode.php
     
    The Chosen Generl, Dec 8, 2009 IP
  8. rahulephp

    rahulephp Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hey thanks buddy
    i will do this and get back to you soon.
    thanks again for your time
     
    rahulephp, Dec 8, 2009 IP
  9. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #9
    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.
     
    Wogan, Dec 11, 2009 IP
  10. rahulephp

    rahulephp Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks Wogan,
    It works
     
    rahulephp, Dec 11, 2009 IP