-2

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

11
  • $arr[0] - $arr[9]?
    – Eddie
    Commented Jan 29, 2018 at 5:49
  • Yes $arr[0] - $arr[10]
    – Zandb
    Commented Jan 29, 2018 at 5:50
  • 2
    Using for loops? Commented Jan 29, 2018 at 5:50
  • generate this array by loops YES
    – Zandb
    Commented Jan 29, 2018 at 5:52
  • the first 10 is array 0 - 9. So print it as $arr[0] and so on. You can use loop to make it easier.
    – Eddie
    Commented Jan 29, 2018 at 5:52

5 Answers 5

10

use array_slice

array_slice($array, 0, 10);

Syntax : array_slice(array,start,length,preserve)

Read - PHP: Extract a slice of the array


One more Dirty trick

for ($i = 0; $i < 10 && $i < count($myArray); $i++) {
    echo $myArray[$i] . PHP_EOL;
}
1
  • 1
    3rd parameter should be 10. as it represents length of desired array. Commented Jan 29, 2018 at 5:52
2

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

0
2

Check to see if array_splice solves your problem.

<?php
    $arr = [1,2,3,4,5];

    $newArr = array_splice($arr, 0, 3);

    echo '<pre>';
    print_r($arr);
    print_r($newArr);

You see here in the above example that I have Spliced out the first three elements from the array - $arr, this would give me a new array - $newArr with those three elements.

The arguments are:

  • The source array -- $arr
  • The starting index -- 0
  • The length (ie. number) of elements you need to slice -- 3

array_splice() method is effective in a way where you need to edit the source array. In our case $arr would then only contain the elements [4,5].

See if this is useful to your scenario.

2
  • Don't just post a link. You should always include an example or it's simply a comment disguised as an answer. Answers are for solutions. Commented Jan 29, 2018 at 6:14
  • Thanks for the guidance Magnus, Editing it and doing the same. Commented Jan 29, 2018 at 6:37
0
<?php 
$array = array('Hello','first','nice','loost','emy','rood',1599,34345,1313,45667,5678,35546,8877,3434,56767,7778,9987,8842,1223);
$i =  0 ;
foreach($array as $key=>$value)
{
    if($i <10){
    echo $value.'<br/>';
    }
    $i++;
}
?>

Output

Hello

first

nice

loost

emy

rood

1599

34345

1313

45667

0
-1

I think this is better.

 # $myarray is the array 
 # you want to search
 $count = 0;
 foreach ( $myarray as $item ) {
   if ($count < 10)
      echo $item;
   $count++;
 }
3
  • If the array only contain 9 or less, then no elements will be printed at all. Commented Jan 29, 2018 at 6:16
  • Yes. Since the question requested for the first 10 values.
    – ikwuje
    Commented Jan 29, 2018 at 6:18
  • Thank you @MagnusEriksson . I just fixed it.
    – ikwuje
    Commented Apr 30, 2018 at 23:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.