mardi 4 août 2015

How to do array_push() dynamically in PHP?

Let say that we are going to add some array to a multidimensional array. I can select the number of arrays which should be added to the main array but what about 99 array? Here is my code that I can change $howmany to add the number of arrays that I need (up to 4 arrays):

$stack = array(array("foo" , "bar"));
    foreach ($rowF as $row) {
        switch ($howmany) {
        case 1:
            array_push($stack, array($row[$column1]));
            break;
        case 2:
            array_push($stack, array($row[$column1], $row[$column2]));
            break;
        case 3:
            array_push($stack, array($row[$column1], $row[$column2], $row[$column3]));
            break;
        case 4:
            array_push($stack, array($row[$column1], $row[$column2], $row[$column3], $row[$column4]));
            break;
        }
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire