PHP 数组字符串化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28798159/
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
PHP array stringify
提问by akinuri
In a lyrics application I'm coding, I'm using an array to print an artists table. The artists array looks like this:
在我编写的歌词应用程序中,我使用数组打印艺术家表。艺术家数组如下所示:
$artists = [
[ "Avril Lavigne" ],
[ "3 Doors Down" ],
[ "Celine Dion" ],
[ "Evanescence" ],
[ "Shania Twain" ],
[ "Green Day" ],
//...
];
Before printing it, I do some modification to the array. I have a folder for each artist that contains the lyrics files. I add the folder names to the $artists
array for later use:
在打印之前,我对数组进行了一些修改。我为每个艺术家都有一个包含歌词文件的文件夹。我将文件夹名称添加到$artists
数组中供以后使用:
$folder_fix = [
[" ", "_" ],
[".", "" ],
["&", "n" ],
];
for ($i = 0; $i < count($artists); $i++) {
$folder_name = strtolower($artists[$i][0]);
for ($k = 0; $k < count($folder_fix); $k++) {
$folder_name = str_replace($folder_fix[$k][0], $folder_fix[$k][1], $folder_name);
}
array_push($artists[$i], $folder_name);
}
Later, I add the album and track count for each artist to the array:
后来,我将每个艺术家的专辑和曲目数添加到数组中:
$lyrics_base = "lyrics/";
for ($i = 0; $i < count($artists); $i++) {
$albums_path = $lyrics_base . $artists[$i][1] . "/*";
$tracks_path = $lyrics_base . $artists[$i][1] . "/*/*";
$albums = count(glob($albums_path));
$tracks = count(glob($tracks_path));
array_push($artists[$i], $albums);
array_push($artists[$i], $tracks);
}
The end result of the array looks like this:
数组的最终结果如下所示:
$artists = [
[ "Avril Lavigne", "avril_lavigne", 5, 61 ],
[ "3 Doors Down", "3_doors_down", 5, 13 ],
[ "Celine Dion", "celine_dion", 7, 22 ],
[ "Evanescence", "evanescence", 4, 10 ],
[ "Shania Twain", "shania_twain", 3, 12 ],
[ "Green Day", "green_day", 8, 26 ],
//...
];
Now, my problem is that this process happens every time I visit the page. The 2nd, 3rd, and the 4th columns are created again and again. I think this is redundant.
现在,我的问题是每次访问该页面时都会发生此过程。第 2、3、4 列一次又一次地创建。我认为这是多余的。
I want to save the end result of the array and use it on the page. If this was JavaScript I'd use JSON.stringify()
, but in PHP I don't know how to get the end result of the array. print_r()
doesn't do the job, because it prints it like this:
我想保存数组的最终结果并在页面上使用它。如果这是我会使用的 JavaScript JSON.stringify()
,但在 PHP 中我不知道如何获得数组的最终结果。print_r()
不做这项工作,因为它像这样打印:
Array
(
[0] => Array
(
[0] => Avril Lavigne
[1] => avril_lavigne
[2] => 5
[3] => 61
)
[1] => Array
(
[0] => 3 Doors Down
[1] => 3_doors_down
[2] => 5
[3] => 13
)
...
I want it like this:
我想要这样:
[
[
"Avril Lavigne",
"avril_lavigne",
5,
61
],
[
"3 Doors Down",
"3_doors_down",
5,
13
],
//...
]
Is there a way to print the array the JSON.stringify()
way?
有没有办法以这种方式打印数组JSON.stringify()
?
回答by Nik Drosakis
Is this what you want?
这是你想要的吗?
echo json_encode($artists)
回声 json_encode($artists)