Skip to content
Advertisement

How to explode csv array in php?

I’m pulling 5 random rows from a csv file such as an image, year, hint text and caption text:

<?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";
    }
?>

My output which is correct and how I want it:

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>'
},
];

But I was wondering if it was possible to list each year $data[0] and echo them individually for future use elsewhere? Like explode it or something?

Like:

<ul>
<li>1898</li>
<li>1845</li>
<li>1822</li>
<li>1904</li>
<li>1912</li>
</ul>

Advertisement

Answer

just walk through the array

$years = [];
foreach ($rand as $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    if (!in_array($data[0], $years)) {
        $years[] = $data[0];
    }     
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement