数组到字符串 PHP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7490488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array to String PHP?
提问by Philip Kirkbride
What is the best method for converting a PHP array into a string?
I have the variable $type
which is an array of types.
将 PHP 数组转换为字符串的最佳方法是什么?
我有一个变量$type
,它是一个类型数组。
$type = $_POST[type];
I want to store it as a single string in my database with each entry separated by |
:
我想将它作为单个字符串存储在我的数据库中,每个条目用|
以下符号分隔:
Sports|Festivals|Other
体育|节日|其他
回答by Jakub
You can use json_encode()
您可以使用json_encode()
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
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_decode()从您的数据库解码字符串。 其他什么都没用,JSON 保持数组关系完好,以备后用!
回答by sumit
json_encode($data) //converts an array to JSON string
json_decode($jsonString) //converts json string to php array
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
为什么 JSON :您可以在大多数编程语言中使用它,由 php 的 serialize() 函数创建的字符串仅在 PHP 中可读,并且如果数据库在编写的应用程序之间共享,您将不喜欢将此类内容存储在数据库中不同的编程语言
回答by Incognito
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.
您可以使用,serialize()
但这会使您的数据更难搜索、更难处理并浪费空间。
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.
您拥有 DB 的全部原因是您可以轻松完成这样的工作。您不需要一个表来存储数组,您需要一个可以表示为数组的表。
Example:
例子:
id | word
1 | Sports
2 | Festivals
3 | Classes
4 | Other
You would simply select the data from the table with SQL, rather than have a table that looks like:
您只需使用 SQL 从表中选择数据,而不是具有如下所示的表:
id | word
1 | Sports|Festivals|Classes|Other
That's not how anybody designs a schema in a relational database, it totally defeats the purpose of it.
这不是任何人在关系数据库中设计模式的方式,它完全违背了它的目的。
回答by vvkatwss vvkatwss
One of the Best way:
最好的方法之一:
echo print_r($array, true);
回答by timdev
内爆():
<?php
$string = implode('|',$types);
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()
如果您对序列化一窍不通,您也可以考虑使用json_encode()
回答by Ante Braovic
This one saves KEYS & VALUES
这个保存了KEYS & VALUES
function array2string($data){
$log_a = "";
foreach ($data as $key => $value) {
if(is_array($value)) $log_a .= "[".$key."] => (". array2string($value). ") \n";
else $log_a .= "[".$key."] => ".$value."\n";
}
return $log_a;
}
Hope it helps someone.
希望它可以帮助某人。
回答by Akbar Mirsiddikov
$data = array("asdcasdc","35353","asdca353sdc","sadcasdc","sadcasdc","asdcsdcsad");
$string_array = json_encode($data);
now you can insert this $string_array value into Database
现在您可以将此 $string_array 值插入到数据库中
回答by Guilherme Nascimento
For store associative arrays you can use serialize
:
对于存储关联数组,您可以使用serialize
:
$arr = array(
'a' => 1,
'b' => 2,
'c' => 3
);
file_put_contents('stored-array.txt', serialize($arr));
And load using unserialize
:
并加载使用unserialize
:
$arr = unserialize(file_get_contents('stored-array.txt'));
print_r($arr);
But if need creat dinamic .php
files with array (for example config files), you can use var_export(..., true);
, like this:
但是如果需要.php
使用数组创建动态文件(例如配置文件),您可以使用var_export(..., true);
,如下所示:
Save in file:
保存在文件中:
$arr = array(
'a' => 1,
'b' => 2,
'c' => 3
);
$str = preg_replace('#,(\s+|)\)#', ')', var_export($arr, true));
$str = '<?php' . PHP_EOL . 'return ' . $str . ';';
file_put_contents('config.php', $str);
Get array values:
获取数组值:
$arr = include 'config.php';
print_r($arr);
回答by Guilherme Nascimento
You can use the PHP string function implode()
您可以使用 PHP 字符串函数 implode()
Like,
喜欢,
<?php
$sports=$_POST['sports'];;
$festival=$_POST['festival'];
$food=$_POST['food'];
$array=[$sports,$festival,$food];
$string=implode('|',$array);
echo $string;
?>
If, for example,
$sports='football';
$festival='janmastami';
$food='biriyani';
例如,如果
$sports='football';
$festival='janmastami';
$food='biriyani';
Then output would be:
然后输出将是:
football|janmastami|biriyani
football|janmastami|biriyani
For more details on PHP implode() function refer to w3schools
有关 PHP implode() 函数的更多详细信息,请参阅w3schools