Mick Chertend
Mick Chertend

Reputation: 45

Remove values from array

I have array:

$array = array('aaa', 'bbb', 333, 'ddd', 555, '666');

I would like remove all values where key is > 3;

How is the best way for this?

Upvotes: 1

Views: 153

Answers (4)

Priya
Priya

Reputation: 1554

array_splice($array, 3);

May be it will be the easiest way.

Upvotes: 1

Vitalii Zurian
Vitalii Zurian

Reputation: 17976

You can use foreach loop

foreach($array as $key => $image) {
    if($value > 3) {
        unset($array[$key]);
    }
}

Upvotes: 3

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29975

$array = array_slice($array, 0, 3);

Upvotes: 9

Oussama Jilal
Oussama Jilal

Reputation: 7739

You can use array_slice() see documentation here.

Upvotes: 3

Related Questions