如何使用PHP在Mysql中存储多维数组

jan*_*jua 0 php mysql loops

嗨,我有一个由用户订购的服务数组,如下面打印的$ _POST值

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
)
Run Code Online (Sandbox Code Playgroud)

我很困惑如何存储这些.我用php形式制作了这些数组,以便像下面这样制作他们的组

对于订单输入字段

<input name="<?php echo $service; ?>[]" type="text" id="order<?php echo $service; ?>" size="4">
Run Code Online (Sandbox Code Playgroud)

服务复选框以检查要采取的服务

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

而对于Services WebURL,下面的输入字段就像

<input name="<?php echo $service; ?>[]" type="text" id="<?php echo $service; ?>" size="40">
Run Code Online (Sandbox Code Playgroud)

我很困惑如何只获取具有值的数组并仅存储这些服务的订单号,如显示升序或降序,以及他们的webrurls对每个选中的复选框.

我很困惑如何循环并存储在mysql中.

请帮我.

非常感谢

lin*_*asy 7

您可以利用PHP的帮助json_encode()json_decode()功能来解决这些问题.

要存储和数组mysql,您应该使用json_encode($yourArray);并且应该将返回的字符串存储到mysql.

类似地,对于检索,您应该使用json_decode($yourMySqlStoredString),这将返回给您的数组,您可以使用它进行进一步的操作!

json_encode php函数

json_decode php函数

  • -1糟糕的主意.您应该调整模式以适合数据,而不是将序列化数据推送到RDBMS中. (4认同)

Dej*_*ejv 5

我相信使用serialize()unserialize()函数比使用json_encode()and更好json_decode()

原因很简单:Serialize 将多维(字符串)索引数组编码为字符串,反序列化能够返回相同的数组(原始数组格式),这与 json_encode 和 json_decode 函数的组合不同,后者返回具有数组属性的对象。

嗯,这根本不是很“程序员友好”。

原始数组:

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
    )
)
Run Code Online (Sandbox Code Playgroud)

json_encode:

{"prvni":{"druhy":["a","b","c","d","e","f"]},"prvnidruhy":[1,2,3,4,5]}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

连载:

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;}}
Run Code Online (Sandbox Code Playgroud)

反序列化:

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
Run Code Online (Sandbox Code Playgroud)