duanchui1955 2017-08-14 03:11
浏览 8
已采纳

检查数组中的值是否与另一个爆炸值重复

hello try to find the duplicated value in array i have array like that

$array = array('1%2','3%4','1%2',1%3);

so i want to find duplicated 2 after % so i used explode inside foreach

foreach($array as $value){
   $s = explode('%',$value);
   if($s[1] == $s[1]){
    echo 'there are duplicated 2';
   }
}

so i want $s[1] check if value after % in array is duplicated is there anyway to do that

  • 写回答

1条回答 默认 最新

  • dt97868 2017-08-14 03:19
    关注

    You will need to collect the right side values as you loop through them and check for duplicates. I am accessing the strings' right side values via their "offset" [2]. When found, you can exit the loop with a break.

    Code: (Demo)

    $array=['1%2','3%4','1%2','1%3'];
    $kept=[];
    foreach($array as $i=>$v){
        if(in_array($v[2],$kept)){
            echo "Element (index $i) containing $v has duplicate right side value.";
            break;
        }
        $kept[]=$v[2];
    }
    

    Output:

    Element (index 2) containing 1%2 has duplicate right side value.
    

    If you want to search for all element that end with %2, you can use preg_grep().

    Code:

    $search=2;
    $array=['1%2','1%3','1%4','3%2','5%2'];
    var_export(preg_grep("/%{$search}$/",$array));
    

    Output:

    array (
      0 => '1%2',
      3 => '3%2',
      4 => '5%2',
    )
    

    Or without regex, it will require more function calls:

    $search=2;
    $array=['21%2','1%3','2%22','1%4','3%21','5%2'];
    var_export(array_filter($array,function($v)use($search){return strpos($v,"%$search")+2===strlen($v);}));
    

    Output:

    array (
      0 => '21%2',
      5 => '5%2',
    )
    

    ...[deep breath] Here is attempted answer #4...

    Code:

    $array=['1%2','1%3','1%4','3%2','5%2'];
    foreach($array as $v){
        $grouped[explode('%',$v)[1]][]=$v;  // use right side number as key
    }
    var_export($grouped);
    

    Output:

    array (
      2 => 
      array (
        0 => '1%2',
        1 => '3%2',
        2 => '5%2',
      ),
      3 => 
      array (
        0 => '1%3',
      ),
      4 => 
      array (
        0 => '1%4',
      ),
    )
    

    If you want to count the right side values in the array:

    $array=['1%2','1%3','1%4','3%2','5%2'];
    $array=preg_replace('/\d+%/','',$array);  // strip the left-size and % from elements
    var_export(array_count_values($array));  // count occurrences
    

    Output:

    // [right side values] => [counts]
    array (
      2 => 3,
      3 => 1,
      4 => 1,
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错