user7461846
user7461846

Reputation:

echo specific array elements

foreach($arrhome as $el){
    echo $el['id'] . '-';
}

Result:

1-2-3-4-5-6-7-8-9-

Now I want to echo only if id > 3

It can be done this way:

if($el['id'] > 3){
    echo $el['id'] . '-';
}

But I want this way:

if($el['id'] < 3){return;}
echo $el['id'] . '-';

Nothing is echoed!

Upvotes: 1

Views: 60

Answers (3)

pr1nc3
pr1nc3

Reputation: 8338

foreach($array as $row){
    if($row < 3){continue;}
    echo $row . '-';
}

I think you confused return with continue

The above code will do for you.

Upvotes: 3

u_mulder
u_mulder

Reputation: 54841

return stops your script/function execution. In your case you should use continue so as to move to next iteration:

foreach($arrhome as $el){
    if($el['id'] < 3) {
        continue;
    }
    echo $el['id'] . '-';
}

Upvotes: 0

BadPiggie
BadPiggie

Reputation: 6359

foreach($arrhome as $el){

    if(!($el['id'] < 3)){

     echo $el['id'] . '-';

    }

}

Upvotes: 0

Related Questions