Search code examples
phparrays

How to get First 10 Values of array with PHP


i have array and i need to print first 10 values of array

MyArray :

    Array

        [0] => Hello
        [1] => first
        [2] => nice
        [3] => loost
        [4] => emy
        [6] => rood
        [7] => 1599
        [8] => 34345
        [9] => 1313
        [10] => 45667
        [11] => 5678
        [12] => 35546
        [13] => 8877
        [14] => 3434
        [15] => 56767
        [16] => 7778
        [17] => 9987
        [18] => 8842
        [19] => 1223

array_slice not solve my problem ! Thanks for your helps


Solution

  • You can apply foreach() with counter:-

    $counter =0;
    foreach($array as $arr){
      if($counter < 10){
         echo $arr.PHP_EOL;
      }
      $counter++;
    }
    

    Output:- https://eval.in/943912