kmar akrout
kmar akrout

Reputation: 365

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 ***. 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

Upvotes: 1

Views: 62

Answers (3)

Rafael
Rafael

Reputation: 1535

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.

Upvotes: 1

nice_dev
nice_dev

Reputation: 17825

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

Upvotes: 0

splash58
splash58

Reputation: 26153

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

Upvotes: 2

Related Questions