Skip to content
Advertisement

How to increment php array?

I’m working on a jQuery drop/drag game and pulling the information from a csv file such as image, year and text which is working:

$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";
}
echo "];";

But where I’m stuck at is that I’m trying to create 5 new arrays using a for loop that increments the variable name and also set the year values into the arrays that has already been pulled from the csv

foreach ($rand as $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    for ($i = 1; $i < 5; $i++) {
        $varToEcho = "correct_dropzone$i"; // become correct_dropzone1,correct_dropzone2,etc.
        echo "var ".$varToEcho." = new Array('".$data[0]."');n"; 
    }   
}

What I’m looking for:

var correct_dropzone1 = new Array('1845');
var correct_dropzone2 = new Array('1835');
var correct_dropzone3 = new Array('1586');
var correct_dropzone4 = new Array('1912');
var correct_dropzone5 = new Array('1969');

But my output is:

var correct_dropzone1 = new Array('1845');
var correct_dropzone2 = new Array('1845');
var correct_dropzone3 = new Array('1845');
var correct_dropzone4 = new Array('1845');
var correct_dropzone1 = new Array('1835');
var correct_dropzone2 = new Array('1835');
var correct_dropzone3 = new Array('1835');
var correct_dropzone4 = new Array('1835');
var correct_dropzone1 = new Array('1586');
var correct_dropzone2 = new Array('1586');
var correct_dropzone3 = new Array('1586');
var correct_dropzone4 = new Array('1586');
var correct_dropzone1 = new Array('1912');
var correct_dropzone2 = new Array('1912');
var correct_dropzone3 = new Array('1912');
var correct_dropzone4 = new Array('1912');
var correct_dropzone1 = new Array('1822');
var correct_dropzone2 = new Array('1822');
var correct_dropzone3 = new Array('1822');
var correct_dropzone4 = new Array('1822');

Advertisement

Answer

You don’t need nested loops, just one loop. Use the index of the loop as to create the number suffix of the variable.

foreach ($rand as $i => $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    $varToEcho = "correct_dropzone" . ($i + 1);
    $valToEcho = json_encode(array($data[0]));
    echo "var $varToEcho = $valToEcho;n";
}

I’ve also shown how you can use json_encode() to convert a PHP value to a JavaScript literal, rather than constructing it by hand.

Also, variables with numeric suffixes is usually a clue that you should be using an array rather than separate variables. You could make correct_dropzone an array.

$dropzones = array();
foreach ($rand as $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    $dropzones[] = array($data[0]);
}
echo "var correct_dropzones = " . json_encode($dropzones) . ";";

Do similarly with cardDeck:

$cardDeck = array();
foreach ($rand as $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    $cardDeck[] = array(
        'image' => $data[1],
        'year' => $data[0],
        'hint' => $data[3],
        'caption' => $data[2]
    );
}
echo "var cardDeck = " . json_encode($cardDeck) . ";";

You can use the same loop for both variables.

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