【问题标题】:Display element of 2nd array whose suffix matched with 1st array显示后缀与第一个数组匹配的第二个数组的元素
【发布时间】:2013-05-12 04:02:44
【问题描述】:

我有两个数组,即:

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

array('beautiful', 'lovely', 'power')

我想打印后缀与第一个数组匹配的第二个数组的内容。即输出应该是lovelybeautiful

我如何在 PHP 中做到这一点?

【问题讨论】:

  • 为此尝试一些正则表达式。
  • 而你到目前为止尝试的是?
  • 顺序重要吗?输出['beautiful', 'lovely'] 是否与['lovely', 'beautiful'] 一样可接受?

标签: php arrays string filtering


【解决方案1】:

试试这个

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

您可以在http://writecodeonline.com/php/在线测试

输出

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

【讨论】:

    【解决方案2】:

    这应该会给你你想要的,假设顺序在结果数组中并不重要:

    $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
    )
    */
    

    See Demo

    【讨论】:

      【解决方案3】:

      尝试将array_filter() 与有效回调一起使用。在你的情况下,我建议查看regular expressionspreg_replace()preg_match())。

      <?php
      header('Content-Type: text/plain');
      
      $a = array('beautiful','lovely','power');
      $b = array('ly','ful','ay');
      
      $filters  = array_map(function($filter){ return '/' . $filter . '$/'; }, $b);
      
      $c = array_filter(
           $a,
           function($element)use($filters){ return $element != preg_replace($filters, '', $element); }
           );
      
      var_dump($c);
      ?>
      

      演出:

      array(2) {
        [0]=>
        string(9) "beautiful"
        [1]=>
        string(6) "lovely"
      }
      

      UPDv1:

      preg_match() 的更简短和优化的版本:

      <?php
      header('Content-Type: text/plain');
      
      $a = array('beautiful','lovely','power');
      $b = array('ly','ful','ay');
      
      $filter  = '/^.*(' . implode('|', $b) . ')$/';
      
      $c = array_filter(
           $a,
           function($element)use($filter){ return preg_match($filter, $element); }
           );
      
      var_dump($c);
      ?>
      

      相同的输出。

      【讨论】:

        【解决方案4】:

        这应该可行:

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

        您当然可以优化它并使其更短,但它很容易理解并且是一个很好的起点。

        【讨论】:

        • strripos($word, $suffix) == strlen(str_replace($suffix, '', $word)),因为只有strripos($word, $suffix) 会在任何地方找到后缀。 pfulower 未被过滤掉。
        • 嗯是的。我想你是对的。昨晚我用fullness 对其进行了测试,它没有被过滤。为什么fullness 会被忽略而pfulower 会被选中?
        猜你喜欢
        • 2015-05-23
        • 2022-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多