Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 后缀与第一个数组匹配的第二个数组的显示元素_Php_Arrays_String_Filtering - Fatal编程技术网

Php 后缀与第一个数组匹配的第二个数组的显示元素

Php 后缀与第一个数组匹配的第二个数组的显示元素,php,arrays,string,filtering,Php,Arrays,String,Filtering,我有两个阵列,即: array('ly', 'ful', 'ay') 及 我想打印后缀与第一个数组匹配的第二个数组的内容。i、 e.输出应该是可爱的,漂亮的 如何在PHP中实现这一点?假设结果数组中的顺序不重要,这应该会满足您的需要: $arr1 = ['ly', 'ful', 'ay']; $arr2 = ['beautiful', 'lovely', 'power']; $result = array_filter($arr2, function($word) use ($arr1){

我有两个阵列,即:

array('ly', 'ful', 'ay')

我想打印后缀与第一个数组匹配的第二个数组的内容。i、 e.输出应该是
可爱的
漂亮的


如何在PHP中实现这一点?

假设结果数组中的顺序不重要,这应该会满足您的需要:

$arr1 = ['ly', 'ful', 'ay'];
$arr2 = ['beautiful', 'lovely', 'power'];

$result = array_filter($arr2, function($word) use ($arr1){
    $word_length = strlen($word);
    return array_reduce($arr1, function($result, $suffix) use ($word, $word_length) {
        if($word_length > strlen($suffix))
            $result = $result || 0 === substr_compare($word, $suffix, -strlen($suffix), $word_length);
        return $result;
    }, false);
});

print_r($result);

/*
Array
(
    [0] => beautiful
    [1] => lovely
)
*/

假设结果数组中的顺序不重要,则这应该会满足您的需求:

$arr1 = ['ly', 'ful', 'ay'];
$arr2 = ['beautiful', 'lovely', 'power'];

$result = array_filter($arr2, function($word) use ($arr1){
    $word_length = strlen($word);
    return array_reduce($arr1, function($result, $suffix) use ($word, $word_length) {
        if($word_length > strlen($suffix))
            $result = $result || 0 === substr_compare($word, $suffix, -strlen($suffix), $word_length);
        return $result;
    }, false);
});

print_r($result);

/*
Array
(
    [0] => beautiful
    [1] => lovely
)
*/
试试这个

$suffix=array('ly','ful','ay');
$words = array('beautiful','lovely','power');
$finalarray=array();
foreach($words as $word)
{
    foreach($suffix as $suff)
    {
       $pattern = '/'.$suff.'$/';
       if(preg_match($pattern, $word))
       {
           $finalarray[]=$word;
       }
    }
}
print_r($finalarray);
你可以在网上考试

输出

Array ( [0] => beautiful [1] => lovely ) 
试试这个

$suffix=array('ly','ful','ay');
$words = array('beautiful','lovely','power');
$finalarray=array();
foreach($words as $word)
{
    foreach($suffix as $suff)
    {
       $pattern = '/'.$suff.'$/';
       if(preg_match($pattern, $word))
       {
           $finalarray[]=$word;
       }
    }
}
print_r($finalarray);
你可以在网上考试

输出

Array ( [0] => beautiful [1] => lovely ) 
这应该起作用:

$suffixes = array('ly','ful','ay');
$words = array('beautiful','lovely','power');

foreach($suffixes as $suffix){
    foreach($words as $word){
        if(strripos($word, $suffix) == strlen(str_replace($suffix, '', $word))){
            $results[] = $word;   
        }
    }
}

print_r($results);
您当然可以对此进行优化并缩短,但这很容易理解,也是一个很好的起点。

这应该可以:

$suffixes = array('ly','ful','ay');
$words = array('beautiful','lovely','power');

foreach($suffixes as $suffix){
    foreach($words as $word){
        if(strripos($word, $suffix) == strlen(str_replace($suffix, '', $word))){
            $results[] = $word;   
        }
    }
}

print_r($results);
您当然可以对此进行优化并使其更短,但这很容易理解,而且是一个很好的起点。

尝试使用有效回调。在你的情况下,我建议看看(或)


UPDv1:

更简短、更优化的版本,包括:


相同的输出。

尝试与有效回调一起使用。在你的情况下,我建议看看(或)


UPDv1:

更简短、更优化的版本,包括:



相同的输出。

为此尝试一些正则表达式。到目前为止您尝试的是?顺序重要吗?输出<代码>['beauty','beauty']是否与<代码>['beauty','beauty']]一样可以接受?为此,请尝试一些正则表达式。到目前为止,您尝试过的是?顺序重要吗?输出
['beauty','beauty']
['beauty','beauty']
一样可以接受吗?
stripos($word,$suffix)==strlen(str_replace($suffix,,$word))
,因为只要
stripos($word,$suffix)
就可以在任何地方找到后缀<代码>pfulower没有被过滤掉。嗯,是的。我想你是对的。昨晚我用
丰满度测试了它,但没有过滤。为什么要忽略
full
而选择
pfulower
strripos($word,$suffix)==strlen(str_replace($suffix,,$word))
,因为只要
strripos($word,$suffix)
就可以在任何地方找到后缀<代码>pfulower
没有被过滤掉。嗯,是的。我想你是对的。昨晚我用
丰满度测试了它,但没有过滤。为什么会忽略
full
而选择
pfulower