Skip to content
Advertisement

How to remove empty array from multidimensional array in php?

Currently I’m stuck and lost with trying to remove empty arrays from a csv file and actually don’t understand how I’m pulling up an empty array. I’m pulling 5 random rows from a csv file which works but I keep getting only 1 blank array after a shuffle.

My output:

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

After looking around I thought array_map and array_filter would work but it clears my whole array

$r= array_map('array_filter', $r);
$r= array_filter( $r);

My code:

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

Advertisement

Answer

Decided to not go furter with using PHP arrays, just going to call it using js arrays

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement