1

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 _/_

3
  • Don't use the same variable name $data for both arrays. Commented Apr 8, 2017 at 9:58
  • so then how should I append it? I mean to say where is my logic failing and at what point should I implement the new variable so that even if there are more than 2 arrays they can be added easily. Commented Apr 8, 2017 at 10:01
  • It's all explained in my answer. Commented Apr 8, 2017 at 10:05

3 Answers 3

1

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"];
    }   
Sign up to request clarification or add additional context in comments.

4 Comments

new Array() in PHP?
one more question @Dez what i want is i have created an array like this $data[$item['id']] = array() which means it will create an array value with name whatever the value is in $item['id'], What i want to do is i want to create a last array with name total to store the total amount in it. how to achieve so ?
Could you elaborate please? Anyway, you should create a new question independently with your new found doubts.
Oh, I think I got what you meant. After the loop, you can add a new element to the array. For example in your case: $data['total'] = array( 'price' => $total_amt);
1

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();

Comments

0

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"]
    );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.