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

Asked

Viewed 41 times

1

The thing is, I’m making a small system where the person can include in an array the desired products, he clicks on the "Insert" product button and some information is collected from that product and stored in an array, in this case, the "id_product", "product name", "product preco_tag", "quantity_product" is the information collected and saved and that goes on, the person will insert products and they will be 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 will be the key of the array? If I leave it the way it is, it will insert so

    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)

However I do not want to be these default numbers (0, 1, 2, 3...), I want to be the "id_product" the key of the array, for later when I delete a product, I take the "id_prod", compare with the key of the array and where it is equal I give "unset", ie, I will be removing. I need to make the array key equal to the id_product, how do I do this? Thank you!

  • Place the code responsible for filling the array as you have it now please Gabriel, to get a more targeted solution to your problem

  • 1

    I’m new to stackoverflow, I was looking for the edit button. Now it’s written.

  • Welcome, when you can take a look here: http://answall.com/tour

  • It seems to me that already happens on this line: $arrayProduto[$retorno[$dados]->id_produto], this $retorno[$dados]->id_produto is the right product id? Already be injecting as key in the $arrayProduto

  • Yes, that part of $retorno[$dados]->id_produto is the id of the product chosen, but the following happens, see in the second code box, it is like this: array (size=3) 0 => .... 1 => ... and see that the 'id_prod' => string '1' (length=1) has value "1", I would like this value of id_prod to be the value of the array key.

  • It’s weird, and this code box is the result var_dump($arrayProduto);?

  • Correct, I posted a var_dump($arrayProduct) to make it easier to view. See in this same box the following part: array (size=3) 0 =>... 1 => ... 2 =>... I wish these keys were equal to the value of the field 'id_prod', in case it would look like this: array (size=3) 1 =>... 5 => ... 2 =>... (The fact that 2 was equal to 2 was just coincidence). It was clear my wish/doubt?

  • This "size" does not appear to me doing the var_dump(..., http://php.net/manual/en/function.var-dump.php or here in the examples

  • Wow, since I started developing small things in PHP using wamp/sublime/mysql Workbench appears to me this "size" and tbm the "length", you think it might interfere with something?

  • No, I don’t think that’s relevant

Show 5 more comments
No answers

Browser other questions tagged

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