duanboniao5903
duanboniao5903
2018-08-08 13:33

PHP中的字符串数组(检查2个或更多字符串是否具有相同的开头)

已采纳

My question seems a little bit silly, but I don't know why I can't seem to make the code work.

I have an array of strings and want to do the following:

When I find 2, 3 or n elements starting with the same 3 letters exp 09_ I want to make it one element seperated by ***. i tried to do this

for ($j=0;$j<count($list1)-1;$j++)
    { 
        $begin= substr($list1[$j], 0, 3).'<br>';
        //echo $list1[$j].'<br>';


        if( strpos( $list1[$j], $begin ) !== false) 
        {
            $list1[$j]=$list1[$j].'***'.$list1[$j+1];
          unset($list1[$j+1]);
          $list1 = array_values($list1);
        }

        echo $list1[$j].'<br>';
    }

I have this array:

('01_order','09_customer bla bla bla ','09_customer bla1 bla1 bla1','09_customer bla2 bla2 bla2')

I want to make it look like this array

('01-order','09_customer bla bla bla * 09_customer bla1 bla1 bla1* 09_customer bla2 bla2 bla2')

Please give me some ideas

Thank you

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duanbangzhou7809 duanbangzhou7809 3年前

    Make a temporary array indexed by substrings and implode the new array's items

    $res = [];
    
    foreach($list1 as $x) { 
       $res[substr($x, 0, 3)][] = $x;
    }
    
    
    foreach($res as &$x) {
       $x = implode('***', (array) $x);
    }
    
    print_r($res);
    

    demo on eval

    点赞 评论 复制链接分享
  • dongsechuan0535 dongsechuan0535 3年前

    I must confess I really like the @splash58 solution. But I think the number or prefix before the underscore might vary in size, which is why I would do something like this, using his answer with an adaptation.

    $res = [];
    
    foreach($list1 as $x) {
       preg_match('/^(\w+_)/', $x, $matches);
       $prefix = $matches[1];
    
       $res[$prefix][] = $x;
    }
    
    
    foreach($res as &$x) {
       $x = implode('***', (array) $x);
    }
    
    print_r($res);
    

    Hat's off to @splash58.

    点赞 评论 复制链接分享
  • dqtm8504 dqtm8504 3年前
    <pre>
    <?php 
    
    $arr = ['01_order','09_customer bla bla bla ','09_customer bla1 bla1 bla1','09_customer bla2 bla2 bla2','01_order12','02_order'];
    
    function joinSimilarPrefixes($arr){
        $modified_values = [];
        $prefiexs_hash = [];
    
        foreach($arr as $each_value){
            $matches = [];
            if(preg_match("/^(\d\d_).+$/",$each_value,$matches)){
                $location = 0;
                if(isset($prefiexs_hash[$matches[1]])){
                    $location = $prefiexs_hash[$matches[1]];
                }else{
                    $location = count($modified_values);
                    $prefiexs_hash[$matches[1]] = $location;
                    $modified_values[$location] = [];
                }
    
                $modified_values[$location][] = $each_value;
            }
        }
    
        foreach($modified_values as $each_key => $each_collection){
            $modified_values[$each_key] = implode("***",$each_collection);
        }
    
        return $modified_values;
    }
    
    
    print_r(joinSimilarPrefixes($arr));
    

    OUTPUT

    Array
    (
        [0] => 01_order***01_order12
        [1] => 09_customer bla bla bla ***09_customer bla1 bla1 bla1***09_customer bla2 bla2 bla2
        [2] => 02_order
    )
    
    点赞 评论 复制链接分享