简体   繁体   English

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

[英]Array of Strings in PHP (check if 2 or more strings have the same beginning)

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 *** . 当我发现23n开始用相同的3个字母的元素exp 09_我想让它通过分隔一个元素*** 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 谢谢

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 在eval上演示

I must confess I really like the @splash58 solution. 我必须承认我真的很喜欢@ splash58解决方案。 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. 帽子是关于@ splash58的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM