2016-03-08 69 views
1

後如何獲得一些元素的數組的foreach每次經過 像PHP得到一個數字數組元素的所有的foreach

foreach(array(20 elements in database) { 
     echo $element[1]; 
     echo $element[2]; 
     echo $element[3]; 
     echo $element[4]; 
     echo $element[5]; 
    } 
+2

不知道你問什麼 – 2016-03-08 00:21:01

+0

thnx的答覆..我不wnt數組的結果,但結果3 3 ...例如,如果我有一個rray的9個元素..我想要foreach 3次,每次獲得3個元素值.. thnx – It0007

+0

使用array_chunk() – devpro

回答

1
foreach($elements as $currentIndex => $element) { 
    var_dump($currentIndex); # "number" of element for simple (not assoc) arrays 
} 
+0

thnx for reply ..我不wnt結果的數組,但結果3 3 ...例如,如果我有一個9個元素的數組..我想要foreach 3次,每次獲得3個元素值 – It0007

4

不知道你需要什麼實際。根據你的意見,如果您有9個指標陣列,並希望通過3獲得指數3,你可以使用array_chunk():

$yourArr = array(1,2,3,4,5,6,7,8,9); 
$result = array_chunk($yourArr, 3); 
echo "<pre>"; 
print_r($result); 

結果:

Array 
(
    [0] => Array 
     (
      [0] => 1 
      [1] => 2 
      [2] => 3 
     ) 

    [1] => Array 
     (
      [0] => 4 
      [1] => 5 
      [2] => 6 
     ) 

    [2] => Array 
     (
      [0] => 7 
      [1] => 8 
      [2] => 9 
     ) 

) 
相關問題