Akshay Shrivastav
Akshay Shrivastav

Reputation: 1227

PHP: How to append an array inside an array that already has an array value present

I am having the Following code:

$counter = $total_amt = 0;
$data = "";

$address = array(
                    'name'       => filter_var($_POST["cust_name"], FILTER_SANITIZE_STRING),
                    'contact'    => filter_var($_POST["cust_contact"], FILTER_SANITIZE_NUMBER_INT),
                    'pincode_id' => filter_var($_POST["cust_pincode"], FILTER_SANITIZE_NUMBER_INT),
                    'address'    => filter_var($_POST["cust_address"], FILTER_SANITIZE_STRING),
                    'landmark'   => filter_var($_POST["cust_landmark"], FILTER_SANITIZE_STRING),
                    'state_id'   => filter_var($_POST["cust_state"], FILTER_SANITIZE_STRING),
                    'city_id'    => filter_var($_POST["cust_city"], FILTER_SANITIZE_STRING)
                );

foreach ($_SESSION["cart_item"] as $item)
    {
        if($counter < 1)
            {
                $data = array(
                                'item'     => $item['id'],
                                'quantity' => $item["quantity"],
                                'price'    => $item["price"]
                             );
            }
        elseif($counter >= 1)
            {
                $data = array(
                                'item'     => $item['id'],
                                'quantity' => $item["quantity"],
                                'price'    => $item["price"]
                             );
                $data[] = $data;
            }

        $total_amt = $total_amt + $item["final_price"];
        $counter ++;
    }

I am getting following output of the code:

Array
  (
    [item] => 3
    [quantity] => 1
    [price] => 810
      [0] => Array
        (
           [item] => 3
           [quantity] => 1
           [price] => 810
        )

 )

I am expecting this kind of O/P

Array
  (
      [0] => Array
        (
           [item] => 2
           [quantity] => 2
           [price] => 670
        )
      [1] => Array
        (
           [item] => 3
           [quantity] => 1
           [price] => 810
        )

 )

What's happening is i have two products added in cart using php sessions. I want to add that session variable to and array and then store it in database using serialize function. But, i am not able to store the variables in array format as shown above.

I have even tried the array_merge() function and othe logics but none is giving me this output. Can anyone help me out with this logic. Thanks in advance _/_

Upvotes: 1

Views: 41

Answers (3)

Dez
Dez

Reputation: 5838

Modify your script like this. You also get rid of useless code.

$data = array();
...

foreach ($_SESSION["cart_item"] as $item)
    {

         $data[] = array(
             'item'     => $item['id'],
             'quantity' => $item["quantity"],
             'price'    => $item["price"]
         );
        $total_amt = $total_amt + $item["final_price"];
    }   

Upvotes: 1

Barmar
Barmar

Reputation: 780798

The problem is that you're reusing the variable $data.

When you do

$data = array(
                'item'     => $item['id'],
                'quantity' => $item["quantity"],
                'price'    => $item["price"]
             );

it throws array the old $data array, and replaces it with this array.

Then you do:

$data[] = $data;

and it adds a copy of this array to itself.

You can simply write:

$data[] = array(
                'item'     => $item['id'],
                'quantity' => $item["quantity"],
                'price'    => $item["price"]
             );

to add a new element to $data containing this array.

There's also no need for the test if ($counter == 1) to do something different for the first entry. You should just initialize $data to be an empty array before the loop.

$data = array();

Upvotes: 1

Sahil Gulati
Sahil Gulati

Reputation: 15141

Change this to:

if($counter < 1)
{
      $data = array(
           'item'     => $item['id'],
           'quantity' => $item["quantity"],
           'price'    => $item["price"]
      );
}
elseif($counter >= 1)
{
     $data = array(
            'item'     => $item['id'],
            'quantity' => $item["quantity"],
            'price'    => $item["price"]
                  );
     $data[] = $data;
}

This:

if ($counter < 1)
{
    $data[] = array(
        'item' => $item['id'],
        'quantity' => $item["quantity"],
        'price' => $item["price"]
    );
}
elseif ($counter >= 1)
{
    $data[] = array(
        'item' => $item['id'],
        'quantity' => $item["quantity"],
        'price' => $item["price"]
    );
}

Upvotes: 0

Related Questions