如何使用 PHP 在 Mysql 中存储多维数组
您好,我有一系列由用户订购的服务,如下面打印的 $_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以借助 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()
andjson_decode()
functions of PHP solve these things.To store and array into
mysql
, you should usejson_encode($yourArray);
and you should store the returned string intomysql
.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
我相信 使用 serialize() 和 unserialize() 函数比使用 json_encode() 更好和
json_decode()
。原因很简单:Serialize 将多维(字符串)索引数组编码为字符串,而 unserialize 能够返回相同的数组(原始数组格式),这与 json_encode 和 json_decode 函数的组合返回具有数组属性的对象不同。
嗯,这根本就不是很“程序员友好”。
原始数组:
json_encode:
json_decode:
序列化:
反序列化:
I believe the use of serialize() and unserialize() functions is better than using
json_encode()
andjson_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:
json_encode:
json_decode:
serialize:
unserialize:
但是您可以使用 json_decode($data, true) 获取简单的关联数组。
来自文档:
But you could use
json_decode($data, true)
obtaining a simple associative array.From the documentation:
这个答案是基于你最后的评论。您需要循环遍历数组并插入每个值。暂定的...
This answer is based on your last comment. You need to loop through the array and insert each value. Tentative...