skip to Main Content
$files = ['a','b','c','d','e','f'];
$other = ['a','b','c','d','e','f'];

foreach($files as $k => $file){
    foreach($other as $key => $value){
        $data = [
            'column1' => $file,
            'column2' => $value,
        ];
        if($file == $value){
            return false
        }else{
            DB::table('abed')->insert($data);
        }
    }
}

How to make combination of two array without duplicate records and cross duplicate remove

DB insert like that


|column1 |  column2 | 
|------- |  ------- |
|   a    |  b       |       
|   a    |  c       |
|   a    |  d       |
|   a    |  e       |
|   a    |  f       |
|   b    |  a       |
|   b    |  c       |
|   b    |  d       |
|   b    |  e       |
|   b    |  f       |
|   c    |  a       |
|   c    |  b       |
|   c    |  d       |
|   c    |  e       |
|   c    |  f       |
|   d    |  a       |
|   d    |  b       |
|   d    |  c       |
|   d    |  e       |
|   d    |  f       |
|   e    |  a       |
|   e    |  b       |
|   e    |  c       |
|   e    |  d       |
|   e    |  f       |
|   f    |  a       |
|   f    |  b       |
|   f    |  c       |
|   f    |  d       |
|   f    |  e       |

I want like this DB insert

|   a   |   b    |          
|   a   |   c    |
|   a   |   d    |
|   a   |   e    |
|   a   |   f    |
|   b   |   c    |
|   b   |   d    |
|   b   |   e    |
|   b   |   f    |
|   c   |   d    |
|   c   |   e    |
|   c   |   f    |
|   d   |   e    |
|   d   |   f    |
|   e   |   f    |

3

Answers


  1. You used in_array(), refer to the below code :

    $files = ['a','b','c','d','e','f'];
    $other = ['a','b','c','d','e','f'];
    
    foreach($files as $k => $file){
      $parrentArray[] = $file;
      foreach($other as $key => $value){
        $data = [
            'column1' => $file,
            'column2' => $value,
        ];
        if(in_array($value, $parrentArray) == false){
            DB::table('abcd')->insert($data);
        }
      }
    }
    
    Login or Signup to reply.
  2. For this another example

    | a | b |
    | a | c | | a | d | | a | e | | a | f | | b | c | | b | d | | b | e | | b | f |

    $files = ['a','b','c','d','e','f'];
    $other = ['a','b','c','d','e','f'];
    $data = [];
    
    for ($i=0;$i< count($files) ; $i++)
    {
        $counter = $i > 0 ? $i + 1 : 1;
        for ($j=$counter;$j< count($other) ; $j++)
        {
            $data[] = [
                    'column1' => $files[$i],
                    'column2' => $other[$j],
                ];
                
        }   
    }
    

    For another example ,

    column1 column2
    a b
    a c
    a d
    a e
    a f
    b a
    b c
    b d
    b e
    b f
    $files = ['a','b','c','d','e','f'];
    $other = ['a','b','c','d','e','f'];
    $data = [];
    
    for ($i=0;$i< count($files) ; $i++)
    {
        for ($j=0;$j< count($other) ; $j++)
        {
            if($files[$i] != $other[$j]){
                $data[] = [
                        'column1' => $files[$i],
                        'column2' => $other[$j],
                    ];
             
                }
        } 
    }
    
    Login or Signup to reply.
  3. @Vaibhav try this,
    Hope this will helpful for you.
    Also I change in execute query only once instead of execute in loop.

    $files = ['a','b','c','d','e','f'];
    $other = ['a','b','c','d','e','f'];
    $data = [];
    
    for($i = 0; $i < count($files); $i++)
    {
        for($j = $i; $j < count($other); $j++)
        {
            if($files[$i] != $other[$j])
            {
                array_push($data,['column1' => $files[$i], 'column2' => $other[$j]]);
            }
        }   
    }
    echo "<pre>";
    print_r($data);
    DB::table('abcd')->insert($data);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search