PHP:获取与另一个字符串数组的子字符串匹配的字符串数组的数组值

时间:2022-09-25 08:11:05

There are two arrays:

有两个数组:

$strings = array('Apple', 'Banana', 'Orange');
$substrings = array('pp', 'range');

I want to get an array which contains all the strings, that match the substrings:

我想得到一个包含所有匹配子串的字符串的数组:

Array
(
    [0] => Apple
    [2] => Orange
)

Or with new indices:

或者使用新指数:

Array
(
    [0] => Apple
    [1] => Orange
)

5 个解决方案

#1


3  

A simple solution that comes to mind: combine array_filter and strpos;

想到一个简单的解决方案:结合array_filter和strpos;

$strings = array('Apple', 'Banana', 'Orange');
$substrings = array('pp', 'range');

$result = array_filter($strings, function($item) use($substrings) {
  foreach($substrings as $substring)
    if(strpos($item, $substring) !== FALSE) return TRUE;
  return FALSE;
});

To reset indices, you can use the array_values function.

要重置索引,可以使用array_values函数。

#2


1  

$strings = array('Apple', 'Banana', 'Orange');
$substrings = array('pp', 'range');
$newarray = array();

foreach ($strings as $string) {
    foreach ($substrings as $substring) {
        if (strstr($string, $substring)) {
            array_push($newarray, $string);
        }
    }
}

in $newarray you have the result

在$ newarray中你有结果

#3


0  

try array_search. looked at the second note at the bottom of this page http://php.net/manual/en/function.array-search.php for an example.

尝试array_search。查看本页底部的第二个注释http://php.net/manual/en/function.array-search.php作为示例。

#4


0  

$arr=array();
foreach($substrings as $item)
{
    $result=array_keys($strings,$item);
    if ($result)
    {
        $arr=array_merge($arr,$result);
    }
}

#5


0  

You can use array_filter for this:

您可以使用array_filter:

$strings = array('Apple', 'Banana', 'Orange');
$substrings = array('pp', 'range');

$result = array_filter($strings, function($string) use ($substrings){
    foreach($substrings as $substring)
        if(strstr($string, $substring) !== false)
            return true;
    return false;
});

// $result is the result you want.

#1


3  

A simple solution that comes to mind: combine array_filter and strpos;

想到一个简单的解决方案:结合array_filter和strpos;

$strings = array('Apple', 'Banana', 'Orange');
$substrings = array('pp', 'range');

$result = array_filter($strings, function($item) use($substrings) {
  foreach($substrings as $substring)
    if(strpos($item, $substring) !== FALSE) return TRUE;
  return FALSE;
});

To reset indices, you can use the array_values function.

要重置索引,可以使用array_values函数。

#2


1  

$strings = array('Apple', 'Banana', 'Orange');
$substrings = array('pp', 'range');
$newarray = array();

foreach ($strings as $string) {
    foreach ($substrings as $substring) {
        if (strstr($string, $substring)) {
            array_push($newarray, $string);
        }
    }
}

in $newarray you have the result

在$ newarray中你有结果

#3


0  

try array_search. looked at the second note at the bottom of this page http://php.net/manual/en/function.array-search.php for an example.

尝试array_search。查看本页底部的第二个注释http://php.net/manual/en/function.array-search.php作为示例。

#4


0  

$arr=array();
foreach($substrings as $item)
{
    $result=array_keys($strings,$item);
    if ($result)
    {
        $arr=array_merge($arr,$result);
    }
}

#5


0  

You can use array_filter for this:

您可以使用array_filter:

$strings = array('Apple', 'Banana', 'Orange');
$substrings = array('pp', 'range');

$result = array_filter($strings, function($string) use ($substrings){
    foreach($substrings as $substring)
        if(strstr($string, $substring) !== false)
            return true;
    return false;
});

// $result is the result you want.