Merge two database array and delete repeated values

Asked

Viewed 841 times

0

I need urgent help, I’m doing a database query on several different array. When the information with "implode" and picked up again with "explode", however, I get repeated values and I wanted the same function of "SELECT DISTINCT". Does anyone know how to fix 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 https://imgur.com/a/ZmvPb

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 1

    Why are you saving the comma separated values? I believe your case is pertinent to a data normalization

  • I am doing this because in the store, the same product can have more than one color. I see no other way to do this

  • I gave one studied and managed to do the normalization. It was a small tip but it helped a lot!

3 answers

0

I usually use a combination of functions array_merge (to join two or more arrays and return an array) and array_unique (which returns unique values within 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);

Upshot:

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

Observing The tests were done using PHP 7.

0

The way I got it and it worked 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 do 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 inserted 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 take the tag ID, the colors selected by the user and see the quantity:

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

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

    for($x=0;$x<$tamanho;$x++)
    {
      $gravar = "INSERT INTO cod_produtos VALUES ($IdProduto,'$arrayCor[$x]')";
    }
    $conn->query($gravar);
  • If you want to take the last inserted ID, use the following function: http://php.net/manual/mysqli.insert-id.php

-1

I believe that the array_unique can help you after the application of the explode method:

$CorFinal = explode(",",$CorNormal);
$arrayFinal = array_unique($CorFinal);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.