【发布时间】:2019-11-07 15:12:55
【问题描述】:
我正在从 csv 文件中提取 5 个随机行,例如图像、年份、提示文本和标题文本:
<?php
$rows = file('Book1.csv');
$len = count($rows);
$rand = array();
while (count($rand) < 5) {
$r = rand(0, $len);
if (!in_array($r, $rand)) {
$rand[] = $r;
}
}
echo 'var cardDeck = [';
foreach ($rand as $r) {
$csv = $rows[$r];
$data = str_getcsv($csv);
echo "{\n";
echo "'image':'".$data[1]."',\n";
echo "'year':'".$data[0]."',\n";
echo "'hint':'".$data[3]."',\n";
echo "'caption':'".$data[2]."'";
echo "\n},\n";
}
?>
我的输出是正确的以及我想要的输出:
var cardDeck = [{
'image':'<img class="img_card" src="https://www.floridamemory.com/onlineclassroom/game/cards/1898.png">',
'year':'1898',
'hint':'Tampa during the Spanish American War',
'caption':'1898-Early in 1898, President William McKinley sent the battleship Maine to Havana to protect American interests. The war lasted only until August, when the two belligerents signed an armistice. A final treaty was signed in December 1898. <a href="https://www.floridamemory.com/exhibits/floridahighlights/bloxham/">Read More</a>'
},
{
'image':'<img class="img_card" src="https://www.floridamemory.com/onlineclassroom/game/cards/1845.png">',
'year':'1845',
'hint':'Act establishing Florida statehood',
'caption':'1845-The Act establishing statehood for Iowa and Florida was approved on March 3, 1845 by the second session of the 28th Congress. <a href="https://www.floridamemory.com/exhibits/floridahighlights/admitunion/">Read More</a>'
},
{
'image':'<img class="img_card" src="https://www.floridamemory.com/onlineclassroom/game/cards/1822.png">',
'year':'1822',
'hint':'Territory of Florida Established',
'caption':'1822-This first act of Florida\'s Territorial Legislature in 1822 divided the territory into four counties and established local courts. <a href="https://www.floridamemory.com/exhibits/floridahighlights/s222/">Read More</a>'
},
{
'image':'<img class="img_card" src="https://www.floridamemory.com/onlineclassroom/game/cards/1904.png">',
'year':'1904',
'hint':'Mary McLeod Bethune opens her school',
'caption':'1904-Mary McLeod Bethune founded the Daytona Normal and Industrial School for Negro Girls. Her school later merged with the Cookman Institute of Jacksonville in 1923 and today is known as Bethune-Cookman University. <a href="https://www.floridamemory.com/onlineclassroom/marybethune/">Read More</a>'
},
{
'image':'<img class="img_card" src="https://www.floridamemory.com/onlineclassroom/game/cards/1912.png">',
'year':'1912',
'hint':'First train in Key West',
'caption':'1912-January 22, 1912 the first passenger train arrived in Key West, marking the completion of Henry Flagler\'s East Coast Railroad from Jacksonville to the Southernmost City. <a hre="https://www.floridamemory.com/blog/2013/01/22/first-train-to-key-west-january-22-1912/">Read More</a>'
},
];
但我想知道是否可以列出每年 $data[0] 并单独回显它们以供将来在其他地方使用?比如爆炸之类的?
喜欢:
<ul>
<li>1898</li>
<li>1845</li>
<li>1822</li>
<li>1904</li>
<li>1912</li>
</ul>
【问题讨论】:
-
提示:不要在 PHP 代码中手动构建 JSON 字符串。只需构建一个 PHP 数组,然后使用
json_encode()生成它的 JSON 版本。 -
提示 2:在循环之前,创建一个空数组。在循环中,将当前记录的年份添加到数组中。循环之后,您现在有了一个包含您遇到的所有年份的数组。
-
@AlexHowansky 感谢 json_encode 的建议。对于提示 2,您的意思是手动列出年份,如 $newArray=array(); $newArray=[1898,1845,1822,1904,1912];因为我的csv文件以后会有100个或条目
-
不,你可以动态地构建阵列。在循环内部(即,当您处理一行时)只需将
$data[0]添加到数组的末尾。当循环结束时,数组将包含您遇到的每一年的列表。然后您可以使用该数组来构建其他一些输出,例如您的UL列表。