php爆炸csv阵列

我要从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>
xiaoxiaov0 回答:php爆炸csv阵列

只需遍历数组

$years = [];
foreach ($rand as $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    if (!in_array($data[0],$years)) {
        $years[] = $data[0];
    }     
}
本文链接:https://www.f2er.com/3143696.html

大家都在问