Join two bank array and exclude repeated values

0

I need urgent help, I'm doing a query in the database in several different array. When information "implode" and get caught again with "explode", however, I get repeated values and I wanted the same function as "SELECT DISTINCT". Does anyone know how to solve it? Code:

<h4>Cores</h4>
            <?php          
            while($row_mestre = mysqli_fetch_assoc($matrizMestre))
            {
                $CorNormal = $row_mestre["CorProduto"];
                if(strpos($CorNormal,",") == FALSE)
                {
                    $CorFinal = $CorNormal;
                    echo '<input type="checkbox" name="CorConsulta[]" value="$CorFinal[$j]"/>'. $CorFinal.'<br/>';
                }
                else
                {
                    $CorFinal = explode(",",$CorNormal);
                        if (is_array($CorFinal))
                        {   
                            for($j=0;$j<sizeof($CorFinal);$j++)
                            {   
                                echo '<input type="checkbox" name="CorConsulta[]" value="$CorFinal[$j]"/>'. $CorFinal[$j].'<br/>';
                            }
                        }

                 }     
            }  
            ?>
        </div>

Table images and how it is shown link

    
asked by anonymous 26.01.2018 / 16:11

3 answers

0

I usually use a combination of array_merge functions (to join two or more arrays and return an array) and array_unique (which returns the unique values inside an array.

First I use the array_merge function to join the arrays:

<?php


$primeiro_array = ["Laranja", "Limão", "Banana"];

$segundo_array = ["Laranja", "Limão", "Banana"];

$terceiro_array = array_merge($primeiro_array, $segundo_array);

Then I delete the repeated values with array_unique :

$resultado = array_unique($terceiro_array);

print_r($resultado);

Result:

Array (
    [0] => Laranja
    [1] => Limão
    [2] => Banana
)

Note The tests were done using PHP 7.

    
28.01.2018 / 20:08
0

The way I got it and worked out was this:

            <?php          
            while($row_mestre = mysqli_fetch_assoc($matrizMestre))
            {
                $Cor = $row_mestre["CorProduto"];
                if(strlen($Cor)>0)
                {
                    echo '<input type="checkbox" name="CoresdeProduto[]" value="' .$Cor. '"/>'. $Cor .'<br/>';
                }
            }  
            ?>

I make a normal INSERT without the colors:

$queryEnvio = "INSERT INTO tabela (IdProduto,NomeProduto) VALUES (null, '$NomeProduto')";
mysqli_query($conn,$comando);

Here I select the last value entered by the ID and the Color of another color-only table

    $SelectProduto = "SELECT IdProduto,CorProduto FROM produto,corproduto ORDER BY IdProduto DESC LIMIT 1";
    $Select = mysqli_query($conn,$SelectProduto);
    $linha_select = mysqli_fetch_array($Select);

I get the ID of the tabel, the colors selected by the user and I see the quantity:

    $IdProduto= $linha_select["IdProduto"];
    $arrayCor = $_POST["CorProduto"];
    $TamanhoCor= count($arrayCor);

I make a loop of repetition to insert all the colors according to the ID using the 1FN

    for($x=0;$x<$tamanho;$x++)
    {
      $gravar = "INSERT INTO cod_produtos VALUES ($IdProduto,'$arrayCor[$x]')";
    }
    $conn->query($gravar);
    
28.01.2018 / 20:30
-1

I think array_unique can help you after applying the explode method:

$CorFinal = explode(",",$CorNormal);
$arrayFinal = array_unique($CorFinal);
    
28.01.2018 / 19:15