0

I have an array of values, I was wondering how I can parse certain words if the words in my array has the value i'm looking for plus more text?

example:

I'm looking for rest

Array values has

restful
myrestless
rests
resting

What php function, if any, can I use where it will show that there are 4 items that has the word rest?

Thanks in advance!

1 Answer 1

3

Use array_filter:

$array = array('restful', 'myrestless', 'rests', 'resting');

$needle = 'rest';
$rest_array = array_filter( $array, 
    function( $el) use( $needle) { 
        return !(strpos( $el, $needle) === false); 
    }
);
var_dump( $rest_array);

Note that this requires PHP >= 5.3 (maybe 5.4?). Otherwise you will have to rewrite it to not use anonymous functions and closures.

Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.