1

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

4
  • 2
    Can you give an example? Commented Aug 8, 2018 at 13:38
  • Could you put the array you have (or some keys) and what you expect it to look like? I think I understood. Will write an example. Commented Aug 8, 2018 at 13:39
  • 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') Commented Aug 8, 2018 at 13:41
  • 1
    @kmarakrout Add this in your post by editing so that everybody can see. Commented Aug 8, 2018 at 13:42

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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

2 Comments

Merci beaucoup :D
Welcome :) I can make it shorter if it's lengthy to you :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.