1

Ok... right of the batt, let me clear what the question is not about. it is not about in_array.

Because as the PHP manual clearly explains, the function 'in_array' checks if a value exists in an array. But it does this check based on equality. It does not do as based on partial existence.

For example, if the value I'm looking is 'overflow' and I happened have an array like this

array('cnn','stackoverflow'),

the in_array would come back with a FALSE, telling me that overflow does not exist in the in values of this array, which in a way is TRUE. but also in a way, is FALSE.

To me, the string "overflow" do exists in the string stackoverflow". Therefore, it should have returned TRUE. Of course I cannot argue this point much.

Is there another function ( or an efficient one-liner) in PHP to get me what I want?

i'm looking for a solution something like this

array_filter($ary,'strlen');

which removes the empty lines from the $ary in a very efficient way.

I do not want to go thru the traditional way that is to go thru a foreach and do a comparison between the needle and the haystack using strpos. That solution I already know.

I'm looking for a one liner, like in the (strlen) example

Thx.

3
  • You need to cut short your question Commented Nov 6, 2013 at 5:10
  • Sorry Mr. Alien, I'm not sure what you meant by your comment. Commented Nov 6, 2013 at 5:12
  • Are you just looking to see if a word value or value is in the array or part of a string in the array? Commented Nov 6, 2013 at 5:23

2 Answers 2

2

No function available in php which satisfy exact requirement of author. Developer has to write some code so you can try below code:

function array_check($arr, $keyword) {
foreach($arr as $index => $string) {
    if (strpos($string, $keyword) !== FALSE)
        return $index;
  } 
}
 var_dump(array_check(array('cnn','stackoverflow'),'overflow'));
 exit;
Sign up to request clarification or add additional context in comments.

4 Comments

While your answer is working answer, But author already tell he already know the solution using compare in foreach.
no function available in php which satisfy exact requirement of author. Developer has to write some code.
Yes I see the same, If you update your answer with this comment it is more useful for author and others.
thank you bhumi for the working code. definitely saves me the time to go that traditional route. But I feel that one-liner closure driven functions are more efficient. In my question, I was seeking a solution towards that route. I would be inclining towards Ed's solution for that reason. I will test his and mark the final answer. Thank you all.
1

Lame option: false !== strpos(implode($ary, '|'),'overflow') As long as the separator character (| here) isn't in your search string, this works.

More sophisticated option: count(array_filter( $ary, function($x) { return strpos($x, 'overflow'); } ) );

Edit: Second option full code looks like this:

$ary = array('cnn', 'stackoverflow'); // or whatever your data is
(bool) count(array_filter( $ary, function($x) { return strpos($x, 'overflow'); } ) );

The count() value will be 0 if not found, or positive if a match was found. So, you could use it in an if() statement, return it from a function, or whatever.

4 Comments

+1 for second option using one liner array_filter with closure callback.
I tested the example above and it worked; please see my edit.
Yes it is working now i tested it. Just one improvement suggestion add a type cast (bool) to in front of this. After all checking true/false is final aim. (bool)count(array_filter( $ary, function($x) { return strpos($x, 'overflow'); } ) );
Good suggestion; I added the type cast.

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.