整个php数组另存为字符串
是否有一种方法可以使整个数组内容成为一个字符串,这样我就可以保存它。我要保存的字符串与print\r($Array)函数的输出相同整个php数组另存为字符串,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,是否有一种方法可以使整个数组内容成为一个字符串,这样我就可以保存它。我要保存的字符串与print\r($Array)函数的输出相同 Array ( [0982385099] => Array ( [Title] => The Key of Life; A Metaphysical Investigation [ISBN] => 0982385099 [Author] => Randolph J. Roge
Array (
[0982385099] => Array (
[Title] => The Key of Life; A Metaphysical Investigation
[ISBN] => 0982385099
[Author] => Randolph J. Rogers
[SalesRank] => 522631 ...
我希望将这种字符串保存在另一个文件(txt或php文件)上,该文件将由我正在执行的程序生成。将
true
作为第二个参数传递给函数将允许您捕获print\r
的输出
$str = var_export($array, true);
$str = print_r($arr, true);
我会用。这是因为每个浏览器都可以
代码:
您可以使用serialise()函数将数组转换为字符串
e.g. we have an array $arr
$arr = Array(
"0" => "Dipendra",
"1" => "Kshitiz",
"2" => "Kushal",
"3" => "Nirmal",
"4" => "Prabin",
"5" => "Prakash",
"6" => "Sujit"
);
echo serialise($arr);
Now if we use serialise() function for this array we can view the following output
a:7:{i:0;s:8:"Dipendra";i:1;s:7:"Kshitiz";i:2;s:6:"Kushal";i:3;s:6:"Nirmal";i:4;s:6:"Prabin";i:5;s:7:"Prakash";i:6;s:5:"Sujit";}
因此,我们可以将数组用作字符串。您的数据库表结构排序规则(php_my_admin)也应该相应地定义为默认的拉丁语拉丁语1_瑞典语\u ci不适用于我。
我使用了utf8\u bin的方法,与您为多维数组实现serialize()函数的方法相同
{"1":"Hello world!","2":2}
e.g. we have an array $arr
$arr = Array(
"0" => "Dipendra",
"1" => "Kshitiz",
"2" => "Kushal",
"3" => "Nirmal",
"4" => "Prabin",
"5" => "Prakash",
"6" => "Sujit"
);
echo serialise($arr);
Now if we use serialise() function for this array we can view the following output
a:7:{i:0;s:8:"Dipendra";i:1;s:7:"Kshitiz";i:2;s:6:"Kushal";i:3;s:6:"Nirmal";i:4;s:6:"Prabin";i:5;s:7:"Prakash";i:6;s:5:"Sujit";}