开发者

How to Store Multidimensional Array in Mysql Using PHP

Hi I have an array of Services ordered by user like below $_POST values printed

Array
(
[compaignID] => 4
[totalChecked] => 3
[hear_about_us] => Google
[videolink] => 
[LinkedIn] => Array
    (
        [0] => 1
        [1] => 1
        [2] => http://developer.comoj.com
    )

[Facebook] => Array
    (
        [0] => 
        [1] => 
    )

[Twitter] => Array
    (
        [0] => 
        [1] => 
    )

[YouTube] => Array
    (
        [0] => 2
        [1] => 4
        [2] => http://developer.comoj.com
    )

[Coupon_Area] => Array
    (
        [0] => 
        [1] => 
    )

[Website] => Array
    (
        [0] => 
        [1] => 
    )

[Google_Map] => Array
    (
        [0] => 
        [1] => 
    )

[Email] => Array
    (
        [0] => 3
        [1] => 8
        [2] => http://developer.comoj.com
    )

[Share_To_Social_Media] => Array
    (
        [0] => 
        [1] => 
    )

[btnSubmit] => Submit
)

I'm confused how to store these. I made these arrays in php form to make their groups like below

for Orders Input field

<input name="<?php echo $service; ?>[]" type="text" id="order<?php echo $service; ?>" size="4">

for Services Check boxes to check which services to take

<input name="<?php echo $service; ?>[]" type="checkbox" id="show<?php echo $service; ?>" value="<?php echo $serviceID; ?>" onclick="countChecked();">

And for Services WebURL the input field below like

<input name="<?php echo $service; ?>[]" type="text" id="<?php echo $service; ?>" size="40">

I'm confused how to get only arrays which have values and store only these services thei开发者_开发百科r order number like for showing ascending or descending and their webrurls against each checked checkbox.

I'm confused how to loop through and store in mysql.

Please help me.

Many Thanks


You can take help of json_encode() and json_decode() functions of PHP solve these things.

To store and array into mysql, you should use json_encode($yourArray); and you should store the returned string into mysql.

Similarly for retrieving, you should use json_decode($yourMySqlStoredString) and this will return the array back to you, which you can use for your further manupulations!

json_encode php function

json_decode php function


I believe the use of serialize() and unserialize() functions is better than using json_encode() and json_decode().

The reason is simple: Serialize encodes multidimensional (string)indexed array into string and unserialize is able to return the same array (in original array format) unlike the combination of json_encode and json_decode function which returns object with array properties.

Well and that is simply not very "programmer friendly".

Original array:

Array(
[prvni] => Array
    (
        [druhy] => Array
            (
                [0] => a
                [1] => b
                [2] => c
                [3] => d
                [4] => e
                [5] => f
            )
    )

[prvnidruhy] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
    )
)

json_encode:

{"prvni":{"druhy":["a","b","c","d","e","f"]},"prvnidruhy":[1,2,3,4,5]}

json_decode:

object(stdClass)[1]
 public 'prvni' => 
  object(stdClass)[2]
   public 'druhy' => 
    array (size=6)
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
      2 => string 'c' (length=1)
      3 => string 'd' (length=1)
      4 => string 'e' (length=1)
      5 => string 'f' (length=1)
   public 'prvnidruhy' => 
    array (size=5)
      0 => int 1
      1 => int 2
      2 => int 3
      3 => int 4
      4 => int 5

serialize:

a:2:{s:5:"prvni";a:1:{s:5:"druhy";a:6:{i:0;s:1:"a";i:1;s:1:"b";i:2;s:1:"c";i:3;s:1:"d";i:4;s:1:"e";i:5;s:1:"f";}}s:10:"prvnidruhy";a:5:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;}}

unserialize:

array (size=2)
 'prvni' => 
    array (size=1)
 'druhy' => 
    array (size=6)
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
      2 => string 'c' (length=1)
      3 => string 'd' (length=1)
      4 => string 'e' (length=1)
      5 => string 'f' (length=1)
 'prvnidruhy' => 
    array (size=5)
      0 => int 1
      1 => int 2
      2 => int 3
      3 => int 4
      4 => int 5


But you could use json_decode($data, true) obtaining a simple associative array. From the documentation:

When TRUE, returned objects will be converted into associative arrays.


This answer is based on your last comment. You need to loop through the array and insert each value. Tentative...

foreach ($array AS $v) {
$sql = "INSERT INTO `tbl_page_link` (serviceID, sorder, webaddress) VALUES ($v[0], $v[1], '$v[2]')";
mysql_query($sql);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜