Help with arrays and PHP sessions

0

I am working in a shopping cart, so that the customer can access the shopping cart needs to be registered, when the client starts session I create a variable $ _SESSION ['Client'] from which I extract certain data that previously registered, if the customer adds products to his shopping cart, then the variable $ _SESSION ['purchases'] is created. My idea is that when the client clicks on the buy button, this is sent to a php file where the purchase data is inserted into a table, here comes the problem. I want the data of the $ _SESSION ['Client'] to be in every $ _SESSION ['purchases'].

For example, This is the arrray that exists in $ _SESSION ['purchases'].

Array
(
[0] => Array
    (
        [Id] => 4
        [Nombre] => Kit 2
        [Precio] => 259
        [Imagen] => sese.jpg
        [Cantidad] => 1
    )

[1] => Array
    (
        [Id] => 5
        [Nombre] => Kit 3
        [Precio] => 385
        [Imagen] => ripo.png
        [Cantidad] => 1
    )

)

And the data that you get from the client that was given a login is in the following array.

Array
(
[0] => Array
    (
        [Nombre] => Raul Leyva
        [Telefono] => 614-123-4567
        [Ciudad] => chihuas
        [Estado] => chihuas
        [Pais] => mehico
    )

)

What I want to do, would be the following:

Array
(
[0] => Array
    (
        [Id] => 4
        [Nombre] => Kit 2
        [Precio] => 259
        [Imagen] => sese.jpg
        [Cantidad] => 1
        [Nombre] => Raul Leyva
        [Telefono] => 614-123-4567
        [Ciudad] => chihuas
        [Estado] => chihuas
        [Pais] => mehico
    )

[1] => Array
    (
        [Id] => 5
        [Nombre] => Kit 3
        [Precio] => 385
        [Imagen] => ripo.png
        [Cantidad] => 1
        [Nombre] => Raul Leyva
        [Telefono] => 614-123-4567
        [Ciudad] => chihuas
        [Estado] => chihuas
        [Pais] => mehico
    )

)

my doubt is how together the arrays in that way. thanks

    
asked by rulo900 11.08.2018 в 06:48
source

1 answer

0

Putting together the 2 arrays that you put as an example is quite simple, however if you observe, you have two equal indexes " Nombre ". You can not have two equal indexes in the same array. For the test I put one Nombre_art , and the other Nombre_cli . I did not use session variables $_SESSION , but you should have no problem applying the same code to them.

<?php

$compras = array(
array('Id' => '4', 'Nombre_art' => 'Kit 2', 'Precio' => '259', 'Imagen' => 'sese.jpg', 'Cantidad' => '1'), 
array('Id' => '5', 'Nombre_art' => 'Kit 3', 'Precio' => '385', 'Imagen' => 'ripo.png', 'Cantidad' => '1'));
$cliente = array(array('Nombre_cli' => 'Raul Leyva', 'Telefono' => '614-123-4567', 'Ciudad' => 'chihuas', 'Estado' => 'chihuas', 'Pais' => 'mehico'));
$compras_size = count($compras);
for($i=0; $i<$compras_size; $i++) {
    foreach($cliente[0] as $key=>$data) {
        $compras[$i][$key] = $data;
    }
}

print_r($compras);

The exit:

Array
(
    [0] => Array
        (
            [Id] => 4
            [Nombre_art] => Kit 2
            [Precio] => 259
            [Imagen] => sese.jpg
            [Cantidad] => 1
            [Nombre_cli] => Raul Leyva
            [Telefono] => 614-123-4567
            [Ciudad] => chihuas
            [Estado] => chihuas
            [Pais] => mehico
        )

    [1] => Array
        (
            [Id] => 5
            [Nombre_art] => Kit 3
            [Precio] => 385
            [Imagen] => ripo.png
            [Cantidad] => 1
            [Nombre_cli] => Raul Leyva
            [Telefono] => 614-123-4567
            [Ciudad] => chihuas
            [Estado] => chihuas
            [Pais] => mehico
        )

)
    
answered by 11.08.2018 в 07:57