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

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

我的问题似乎有点傻,但我不知道为什么我似乎无法制作代码 p>

我有一个字符串数组,想要执行以下操作: p>

当我找到 2 code>时, 3 code>或 n code>元素以相同的3个字母开头 exp 09 _ code>我想让它成为一个由 *** code分隔的元素 >。 i试图这样做 p>

  for($ j = 0; $ j&lt; count($ list1)-1; $ j ++)
 {
 $ begin  = substr($ list1 [$ j],0,3)。'&lt; br&gt;'; 
 // echo $ list1 [$ j]。'&lt; br&gt;'; 
 
 
 if(strpos)  ($ list1 [$ j],$ begin)!== false)
 {
 $ list1 [$ j] = $ list1 [$ j]。'***'。$ list1 [$ j + 1];  
 unset($ list1 [$ j + 1]); 
 $ list1 = array_values($ list1); 
} 
 
 echo $ list1 [$ j]。'&lt; br&gt;'; 
}  
  code>  pre> 
 
 

我有这个数组: p>

 ('01_order','09_customer bla bla bla','09_customer  bla1 bla1 bla1','09_customer bla2 bla2 bla2'  )
  code>  pre> 
 
 

我想让它看起来像这个数组 p>

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

请给我一些想法 p>

谢谢 p> div>

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

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

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.