csv 如何在php中删除多维数组中的空数组?

kiz8lqtg  于 2023-11-14  发布在  PHP
关注(0)|答案(1)|浏览(124)

目前,我在试图从csv文件中删除空数组时遇到了困难,实际上我不明白我是如何提取空数组的。我从一个csv文件中随机提取了5行,这是可行的,但我在 Shuffle 后只得到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 "];";

knpiaxh1

knpiaxh11#

决定不再使用PHP数组,而是使用js数组调用它

相关问题