开发者

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

开发者 https://www.devze.com 2023-03-10 02:01 出处:网络
I know many questions already asked about this problem, but I was not able to find the right answer for my specific problem.

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($st开发者_运维百科r, $value) !== false) {
            array_push($returnArray, $key);
        }
    }
    return $returnArray;
}


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);


Simply use preg_grep.

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


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


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.


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


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);
?>


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'


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"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消