将平面数组转换为要保存在数据库中的分隔字符串
将 PHP 数组转换为字符串的最佳方法是什么?
我有变量 $type
它是一个类型数组。
$type = $_POST[type];
我想将其作为单个字符串存储在数据库中,每个条目用 |
分隔:
Sports|Festivals|Other
What is the best method for converting a PHP array into a string?
I have the variable $type
which is an array of types.
$type = $_POST[type];
I want to store it as a single string in my database with each entry separated by |
:
Sports|Festivals|Other
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
使用 implode
Use implode
您可以使用 json_encode()
稍后只需使用 json_decode() 从数据库中解码字符串。
其他都没用,JSON 保持数组关系完整以供以后使用!
You can use json_encode()
Later just use json_decode() to decode the string from your DB.
Anything else is useless, JSON keeps the array relationship intact for later usage!
为什么 JSON :您可以在大多数编程语言中使用它,由 PHP 的 Serialize() 函数创建的字符串只能在 PHP 中读取,并且您不会喜欢将这些内容存储在数据库中,特别是如果数据库在用 PHP 编写的应用程序之间共享不同的编程语言
WHY JSON : You can use it with most of the programming languages, string created by serialize() function of php is readable in PHP only, and you will not like to store such things in your databases specially if database is shared among applications written in different programming languages
最好的方法之一:
One of the Best way:
不,您不想像这样将其作为单个字符串存储在数据库中。
您可以使用
serialize()
但这会使您的数据更难搜索、更难使用,并且会浪费空间。您也可以进行其他一些编码,但通常容易出现相同的问题。
您拥有数据库的全部原因是您可以轻松地完成这样的工作。您不需要一个表来存储数组,您需要一个可以表示为数组的表。
示例:
您只需使用 SQL 从表中选择数据,而不是拥有一个如下所示的表:
这不是任何人在关系数据库中设计模式的方式,它完全违背了它的目的。
No, you don't want to store it as a single string in your database like that.
You could use
serialize()
but this will make your data harder to search, harder to work with, and wastes space.You could do some other encoding as well, but it's generally prone to the same problem.
The whole reason you have a DB is so you can accomplish work like this trivially. You don't need a table to store arrays, you need a table that you can represent as an array.
Example:
You would simply select the data from the table with SQL, rather than have a table that looks like:
That's not how anybody designs a schema in a relational database, it totally defeats the purpose of it.
implode():
然而,Incognito 是对的,你可能不想以这种方式存储它——这完全是对关系型数据库的浪费。数据库的力量。
如果您对序列化一心一意,您也可以考虑使用 json_encode()
implode():
However, Incognito is right, you probably don't want to store it that way -- it's a total waste of the relational power of your database.
If you're dead-set on serializing, you might also consider using json_encode()
这可以节省钥匙和钥匙。价值观
希望它对某人有帮助。
This one saves KEYS & VALUES
Hope it helps someone.
现在您可以将此 $string_array 值插入数据库
now you can insert this $string_array value into Database
对于存储关联数组,您可以使用
serialize
:并使用
unserialize
加载:但如果需要创建带有数组的动态
.php
文件(例如配置文件) ),您可以使用var_export(..., true);
,如下所示:保存在文件中:
获取数组值:
For store associative arrays you can use
serialize
:And load using
unserialize
:But if need creat dinamic
.php
files with array (for example config files), you can usevar_export(..., true);
, like this:Save in file:
Get array values:
您可以使用
serialize
:并使用
unserialize
将字符串转换为数组:You can use
serialize
:and use
unserialize
to convert string to array:有很多方法,
最好的方法有两种
there are many ways ,
two best ways for this are
还有另一种方式,使用短数组语法(方括号)的 PHP
var_export()
缩进 4 个空格:采用 此处。
Yet another way, PHP
var_export()
with short array syntax (square brackets) indented 4 spaces:Taken here.
如果您有一个数组(例如 $_POST)并且需要保留键和值:
结果如下:“name=Paul,age=23,city=Chicago”
If you have an array (like $_POST) and need to keep keys and values:
Result like: "name=Paul, age=23, city=Chicago"