dimanche 22 avril 2018

Conversion one dimensional array to two dimensional array in php?

    <?php
$stack = array(1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8);
$myarr = array(array(),array());
$order = 4;
$i = 1;
$j = 1;

foreach($stack as $value){

    $myarr[i][j] = $value;

    if($j % $order == 0){
        $j = 1;
        $i++;
    }else{
         $j++;   
    }


}

echo $myarr[1][2];

for ($i = 1; $i <= $order; $i++){
    for($j = 1; $j <= $order; $j++){

        echo $myarr[i][j];
        echo " ";

        if($j % $order == 0){
            echo "<br/>";
        }
    }
}


?>

*I want to convert one dimensional array to two dimensional with the existing element in php.Seems some problem while retrieving element from two 2d array.




Aucun commentaire:

Enregistrer un commentaire