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

Time:12-22

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

CodePudding user response:

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

CodePudding user response:

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) == true){
        return false
    }else{
        \DB::table('abcd')->insert($data);
    }
  }
}

CodePudding user response:

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],
                ];
         
            }
    } 
}
  • Related