A
A
Anton89892017-07-23 19:00:27
JavaScript
Anton8989, 2017-07-23 19:00:27

How to display a certain number of elements by condition using foreach?

There is an array, it is displayed through foreach, it is required to display not the entire array but the first 2 elements, in total we will offer 5 of them in the array. I would like to see some example on this topic. Thanks in advance))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
riot26, 2019-02-12
@JustStone

The cycle then works, but with the logic of calculating the degree of the problem. Look closely at the differences:

var number = 2;
var power = 10;
var result = 1;

for(i = 0; i < power; i = i + 1){
    result = result*number;
}
console.log(result);

D
D3lphi, 2017-07-23
@Anton8989

$i = 1;
foreach($arr as $item) {
    if ($i > 2) break;
    echo $item;
    $i++;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question