3
$search=array("<",">","!=","<=",">=")
$value="name >= vivek ";

I want to check if $value contains any of the values of the $search array. I can find out using foreach and the strpos function. Without resorting to using foreach, can I still find the answer? If so, kindly help me to solve this problem.

3
  • Posted the easiest answer to this question Commented Jan 2, 2015 at 7:32
  • check this : stackoverflow.com/questions/2124527/… Commented Jan 2, 2015 at 7:47
  • @Vivek +1 for this question Commented Jan 2, 2015 at 8:01

6 Answers 6

2

Explode $value and convert it into array and then use array_intersect() function in php to check if the string does not contain the value of the array.Use the code below

    <?php
    $search=array("<",">","!=","<=",">=");
    $value='name >= vivek ';
    $array = explode(" ",$value);

    $p = array_intersect($search,$array);
    $errors = array_filter($p);
//Check if the string is not empty
    if(!empty($errors)){
    echo "The string contains an value of array";
    }
    else
    {
    echo "The string does not containe the value of an array";
    }

    ?>

Test the code here http://sandbox.onlinephpfunctions.com/code/7e65faf808de77036a83e185050d0895553d8211

Hope this helps you

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

Comments

0

Yes, but it will require you to re structure your code.

$search = array("<" => 0, ">" => 1,"!=" => 2,"<=" => 3,">=" => 4);

$value = "name => vivek ";

$value = explode(" ", $value);

foreach($value as $val) {
    // search the array in O(1) time
    if(isset($search[$val])) {
       // found a match
    }
}

Comments

0

Use array_map() and array_filter()

function cube($n)
{
    $value="name => vivek ";
    return strpos($value, $n);
//return($n * $n * $n);
}

$a = array("<",">","!=","<=",">=");
$value="name => vivek ";

$b = array_map("cube", $a);
print_r($b);

$b = array_filter($b);

print_r($b);

1 Comment

array_filter accepts a callback, there's no need for array_map here.
0
$search  = array("<",">","!=","<=",">=");
$value="name => vivek ";
foreach($value as $searchval) {
  if(strpos($value,  $searchval) == false)
  {
  echo "match not found";
  }
  else
  {
  echo "match found";
  }
}

Comments

0

Here's a solution using array_reduce:

<?PHP
function array_in_string_callback($carry, $item)
{
    list($str, $found) = $carry;
    echo $str . " - " . $found . " - " . $str . " - " . $item . "<br/>";
    $found |= strpos($str, $item);
    return array($str, (boolean) $found);
}

function array_in_string($haystack, $needle)
{
    $retVal = array_reduce($needle, "array_in_string_callback", array($haystack, false));
    return $retVal[1];
}

$search=array("<",">","!=","<=",">=");
$value="name >= vivek ";

var_dump(array_in_string($value, $search));
?>

Comments

0

My first inclination was to solve the problem with array_walk() and a callback, as follows:

<?php

$search=array("<",">","!=","<=",">=");
$value = "name >= vivek ";

function test($item, $key, $str)
{
    if( strpos($str, $item) !== FALSE ) {
        echo "$item found in \"$str\"\n";        
    }
}

array_walk($search, 'test', $value);
// output:
> found in "name >= vivek "
>= found in "name >= vivek "

Live demo: http://3v4l.org/6B0WX

While this solves the problem without a foreach loop, it answers the question with a "what" rather than a "yes/no" response. The following code directly answers the question and permits answering the "what" too, as follows:

<?php

function test($x)
{
    $value="name >= vivek ";
    return strpos($value, $x);
}
$search = array("<",">","!=","<=",">=");
$chars = array_filter( $search, "test" );
$count = count($chars);
echo "Are there any search chars? ", $answer = ($count > 0)? 'Yes, as follows: ' : 'No.';
echo join(" ",$chars);

// output:
Are there any search chars? Yes, as follows: > >=

Live demo: http://3v4l.org/WJQ5c

If the response had been negative, then the output is 'No.' followed by a blank space.

A key difference in this second solution compared to the first, is that in this case there is a return result that can be manipulated. If an element of the array matches a character in the string, then array_filter adds that element to $chars. The new array's element count answers the question and the array itself contains any matches, if one wishes to display them.

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.