猿问

我想让数组 [1,2,3,4,5,6,7,8,9,10,11] 到数组

我尝试将一个新数组插入到一个数组中


我尝试使用模块化切割它并将其反转


<?php

literal(12);

function literal($sum){

$n;

for($i=0;$i<$sum;$i++){

 $n[] = $i+1;

 echo $n[$i];

 $length = strlen($n[$i]);//count length reset every loop

 $keeper = $n[$i]; //store on new variable reset every loop

 for($m=0;$m<$length;$m++){

   if($keeper>=10){//just skip if below ten

     $newstore[$m] = $keeper%(10^($length-($m+1)));

     $keeper = floor($keeper/10);

   }else {

     $newstore[$m] = $keeper;// this value to keep array and reset it, then keep it again

   }

  }

  $newstored = array_reverse($newstore);//reverse it and reset again

  for($a=0;$a<count($newstored);$a++){

      $fixed[] = $newstored[$a];

  }

}

echo nl2br("\n".count($n)."\n");

for($i=0;$i<count($fixed);$i++){

    echo $fixed[$i];

}

echo nl2br("\n".count($fixed));

}

?>

我想要的[1,2,3,4,5,6,7,8,9,1,0,1,1,1,2] 结果是我得到的实际结果[1,2,3,4,5,6,7,8,9,1,1,0,1,1,0,1,1]


谢谢


Helenr
浏览 110回答 3
3回答

互换的青春

要获得所需的结果,您可以使用1-12的范围并使用implode将其转换为字符串。然后使用str_split创建结果数组。$res = str_split(implode(range(1,12)));print_r($res);结果:Array(&nbsp; &nbsp; [0] => 1&nbsp; &nbsp; [1] => 2&nbsp; &nbsp; [2] => 3&nbsp; &nbsp; [3] => 4&nbsp; &nbsp; [4] => 5&nbsp; &nbsp; [5] => 6&nbsp; &nbsp; [6] => 7&nbsp; &nbsp; [7] => 8&nbsp; &nbsp; [8] => 9&nbsp; &nbsp; [9] => 1&nbsp; &nbsp; [10] => 0&nbsp; &nbsp; [11] => 1&nbsp; &nbsp; [12] => 1&nbsp; &nbsp; [13] => 1&nbsp; &nbsp; [14] => 2)
随时随地看视频慕课网APP
我要回答