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

发布于 2024-11-29 02:47:45 字数 1731 浏览 2 评论 0原文

您好,我有一系列由用户订购的服务,如下面打印的 $_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
)

我很困惑如何存储这些。我以 php 形式制作了这些数组,使它们的组如下所示,

用于服务的订单输入字段

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

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

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

对于服务 WebURL,下面的输入字段就像

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

我很困惑如何仅获取具有值并存储的数组只有这些服务的订单号(例如显示升序或降序)以及针对每个选中的复选框的 weburl。

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

请帮我。

非常感谢

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 their 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街女流氓 2024-12-06 02:47:45

您可以借助 PHP 的 json_encode()json_decode() 函数来解决这些问题。

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

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

json_encode PHP 函数

json_decode PHP 函数

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

喜爱皱眉﹌ 2024-12-06 02:47:45

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

原因很简单:Serialize 将多维(字符串)索引数组编码为字符串,而 unserialize 能够返回相同的数组(原始数组格式),这与 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
    )
)

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

序列化:

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;}}

反序列化:

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

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
清风疏影 2024-12-06 02:47:45

但是您可以使用 json_decode($data, true) 获取简单的关联数组。
来自文档

当为 TRUE 时,返回的对象将转换为关联数组。

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.

迟月 2024-12-06 02:47:45

这个答案是基于你最后的评论。您需要循环遍历数组并插入每个值。暂定的...

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文