Leader
Leader

Reputation: 68

Loosely filtering an array

Let's say I have the following PHP variables:

$colors = array( 'red', 'green', 'blue', 'yellow', 'brown' );
$skey = 'ow';

How can I filter $colors using $skey to get an array which only contain 'yellow' and 'brown'?

Upvotes: 1

Views: 58

Answers (1)

Rikesh
Rikesh

Reputation: 26431

Use array_filter,

$results = array_filter($colors, function($var){ return strpos($var, 'ow') !== false; });

DEMO.

Upvotes: 3

Related Questions