【发布时间】:2019-11-08 20:42:26
【问题描述】:
目前,我在尝试从 csv 文件中删除空数组时陷入困境,实际上我不明白我是如何提取空数组的。我从一个有效的 csv 文件中提取 5 个随机行,但在随机播放后我只得到 1 个空白数组。
我的输出:
var cardDeck = [{
'image':'',
'year':'',
'hint':'',
'caption':''
}
, {
'image':'<img class="card_image" src="https://www.floridamemory.com/onlineclassroom/game/cards/1586.png">',
'year':'1586',
'hint':'Sir Francis Drake attacks Saint Augustine',
'caption':'On May 28 and 29, 1586, Sir Francis Drake led an attack on the Spanish city of St. Augustine. The Englishman commanded a fleet of 25 ships commissioned by Queen Elizabeth to conduct a series of raids against Spanish settlements in the Americas. <a href="https://www.floridamemory.com/blog/2012/05/29/francis-drake-attacks-st-augustine/" target="_blank">Read More</a>'
}
, {
'image':'<img class="card_image" src="https://www.floridamemory.com/onlineclassroom/game/cards/1926.png">',
'year':'1926',
'hint':'Great Miami Hurricane',
'caption':'A catastrophic hurricane made landfall near Miami Beach in the early morning hours of September 18, 1926. Known as the "Great Miami Hurricane," the storm cut a path of destruction across Southern Florida. <a href="https://www.floridamemory.com/exhibits/floridahighlights/hurricane/" target="_blank">Read More</a>'
}
, {
'image':'<img class="card_image" src="https://www.floridamemory.com/onlineclassroom/game/cards/1969.png">',
'year':'1969',
'hint':'First humans on the Moon',
'caption':'On July 20, 1969, Commander Neil Armstrong and Lunar Module Pilot Edwin "Buzz" Aldrin, Jr. landed in the Sea of Tranquility and became the first humans to walk on the moon <a href="https://www.floridamemory.com/onlineclassroom/nasa/photos/" target="_blank">Read More</a>'
}
, {
'image':'<img class="card_image" src="https://www.floridamemory.com/onlineclassroom/game/cards/1822.png">',
'year':'1822',
'hint':'Territory of Florida Established',
'caption':'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/" target="_blank">Read More</a>'
}
];
环顾四周后,我认为 array_map 和 array_filter 可以工作,但它会清除我的整个数组
$r= array_map('array_filter', $r);
$r= array_filter( $r);
我的代码:
<?php
$rows = file('Book1.csv');
$len = count($rows);
$rand = array();
$yearOutput = array(); //array to echo each year individually for future use
while (count($rand) < 5) {
$r = rand(0, $len);
if (!in_array($r, $rand)) {
$rand[] = $r;
}
}
$comma = 1;
$count = count( $rand );
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";
if ( $comma < $count ) echo ", "; //adds comma after last ending brace
++$comma; //adds comma after last ending brace
if (!in_array($data[0], $yearOutput)) {
$yearOutput[] = $data[0];
sort($yearOutput);
}
}
echo "];";
【问题讨论】:
-
在
foreach的顶部,你不能只检查if statementIEif ( $data[1] === ' ' ){中的空白值吗——如果true然后continue;(跳过当前迭代和进入下一个循环迭代)?? -- 我的意思是数组中已经存在空格,你要么必须努力删除它们,要么只是跳过它们 -- 所以开销可以忽略不计。 -
请不要手动构造JSON。构建一个实际的 PHP 数组,然后使用
json_ecode() -
获得随机(技术上,伪随机)列表的更简洁的方法可能是使用
array_fill(),然后使用shuffle() -
@Zak 我做了 if 语句检查是否为空,它有效,但我仍然希望它用一个完整的数组替换那个空
-
@PatrickQ 感谢 JSON 提示