srisar
srisar

Reputation: 1571

How can I find out if a substring exists in a string using PHP?

I know many questions already asked about this problem, but I was not able to find the right answer for my specific problem.

I have a search string - "1|1"

I have a array containing following values - "a" => "1|1", "b" => "2|1,1|1", "c" => "3|2,2|1"

All I want to do is just to find if the search string existed in the array, it should return the array key. If more than one array item has the search string, it should return all of them.

In this example, I am expecting to get both "a" and "b", but when I use strpos(), it only gives me "a".

How can I solve this problem?

Edit**

This is my code

function array_search_i($str, $array)
{
    $returnArray = array();
    foreach ($array as $key => $value) {
        if (strpos($str, $value) !== false) {
            array_push($returnArray, $key);
        }
    }
    return $returnArray;
}

Upvotes: 3

Views: 3854

Answers (8)

anubhava
anubhava

Reputation: 785128

array_filter() is what you need. Try the following code.

$array = array("a"=>array("1|1"), "b"=>array("2|1","1|1"), "c"=>array("1|2"));
var_dump(array_keys(array_filter($array, function ($elem) { return in_array('1|1', $elem); } )));

###OUTPUT

array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
}

Upvotes: 0

Ibu
Ibu

Reputation: 43810

The simplest way I can think of is using the PHP native function array_key().

Here is how it works:

$array = array("a" => "1|1", "b" => "2|1,1|1", "c" => "3|2,2|1");

$result = array_keys($array,'1|1');

print_r($result);
// 'a' => '1|1'

UPDATE:

print_r($array[$result[0]]); // Makes more sense.
                             // '1|1'

Upvotes: 0

konsolenfreddy
konsolenfreddy

Reputation: 9671

Watch out for the !== operator. We have to check for the type as well..

$searchArray = array('2|2','1|1,3|3','1|1');
$search = '1|1';
foreach ($searchArray as $k=> $value) {
    if (strpos($value,$search) !== false) {
        $keysWithMatches[] = $k;
    }
}

print_r($keysWithMatches);

Upvotes: 2

Rakesh Sankar
Rakesh Sankar

Reputation: 9415

You can use array_keys with search_value parameter to search for all values in an array.

This example will get keys of all matching exact values:

<?php
$searchArray = array('2|2','1|1','1|1');
$search = '1|1';
print_r(array_keys($searchArray, $search));
?>

(OR)

This example will get the key of all the partial-values

<?php
foreach($array as $key => $val) {
   if(strpos($val,'green') !== false) {
      $resKeys[] = $key;
   }
}

print_r($resKeys);
?>

Upvotes: 0

Mindaugas
Mindaugas

Reputation: 326

Here you go:

<?php
    $array = Array("a" => "1|1", "b" => "2|1,1|1","c" => "3|1,2|1","d" => Array("a1" => "1|1"));
    array_walk($array,"find");

    function find($value, $key) {
        if (!is_array($value)) {
            if (preg_match("/\b1\|1\b/i",$value)) {
            echo $key .'<br />';
            }
        }
        else {
            array_walk($value,"find");
        }
    }
?>

By this example you will get:

a
b
a1

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86386

Simply use preg_grep.

print_r(preg_grep("/1\|1/", $array));

Upvotes: 2

FilmJ
FilmJ

Reputation: 2011

strpos will only "Find position of first occurrence of a string ".

Look at array_keys. I think this will be closer to what you want. Alternatively, if it's not able to do "partial matching" which is what you probably need, you may have to use array_map or array_filter.

Upvotes: 0

GWW
GWW

Reputation: 44093

$keys = array();
foreach($a as $k=>$v){
    if(strpos($v,'1|1') !== false) $keys[] = $k;
}

Upvotes: 1

Related Questions