How to choose the key of the array in PHP and not that pattern (0 = ... 1 = ... 2 = ...)

1
This is the following, I am making a small system where the person can include in an array the desired products, he clicks the product button "Insert" and some information is taken from that product and stored in an array, in this case, "product_id "" product_name "," product_product "," product_quantity "are the information collected and saved and in that goes, the person inserts products and they are stored in an array. I have the following structure in code:

case 'add':

    $produto = new ProdutoController();
    $retorno = $produto->selectId($_GET['id']);

    if (count($retorno) > 0)
    {
        foreach($retorno as $dados => $value)
        {
            $arrayProduto[$retorno[$dados]->id_produto] = array(
                                                    'id_prod' => $retorno[$dados]->id_produto, 
                                                    'nome_prod' => $retorno[$dados]->nome_produto,
                                                    'code_prod' => $retorno[$dados]->code_produto,
                                                    'preco_prod' => $retorno[$dados]->preco_produto,
                                                    'qtd_prod' => $_POST['txt_quantity']);

        }

        if(!empty($_SESSION['cart_item'])) 
        {    
            foreach ($arrayProduto as $key => $value) 
            {
                if(array_key_exists($key, $_SESSION['cart_item'])) 
                {    
                    ?> 
                        <script language="JavaScript" type="text/javascript"> 
                        alert ("Este produto já foi inserido!") 
                        </script>
                    <?php
                        header("Location: /view/cart/cart.php");
                }
                else
                {
                    $_SESSION['cart_item'] = array_merge($_SESSION['cart_item'], $arrayProduto);
                    header("Location: /view/cart/cart.php");
                }
            }
        }
        else
        {
            $_SESSION['cart_item'] = $arrayProduto;
            header("Location: /view/cart/cart.php");
        }
    }
    else
    {
        header("Location: /view");
    }
    break;

In this case, I have the following doubts: How do I choose which key will be the array? If I leave it the way it is, it inserts like this

    array (size=3)
    0 => 
    array (size=5)
          'id_prod' => string '1' (length=1)
          'nome_prod' => string 'GTX 1050 Gigabyte 2GB 128bits' (length=29)
          'code_prod' => string 'ab01' (length=4)
          'preco_prod' => string '599.90' (length=6)
          'qtd_prod' => string '1' (length=1)
    1 =>
    array (size=5)
          'id_prod' => string '5' (length=1)
          'nome_prod' => string 'VGA GTX 1070 Gigabyte 6GB 256bits' (length=33)
          'code_prod' => string 'ab07' (length=4)
          'preco_prod' => string '1399.90' (length=7)
          'qtd_prod' => string '1' (length=1)
      2 => 
        array (size=5)
          'id_prod' => string '2' (length=1)
          'nome_prod' => string 'VGA GTX 1050Ti EVGA 4GB 128bits' (length=31)
          'code_prod' => string 'ab04' (length=4)
          'preco_prod' => string '699.90' (length=6)
          'qtd_prod' => string '1' (length=1)

But I do not want it to be these default numbers (0, 1, 2, 3 ...), I want the "product_id" to be the array key, so when I delete a product, "id_prod", compare with the array key and wherever I give "unset", that is, I will be removing. I need to make the array key equal to the product_id, how do I do this? Thanks!

    
asked by anonymous 16.11.2016 / 13:42

0 answers