0

My problem is with a an array, that stores all the items inserted in a cart. The cart items have the following structure:

array:3 [▼
  1 => array:7 [▼
    "id" => 1
    "name" => "Listing Sample 1"
    "quantity" => "1"
    "price" => 145.5
    "photo" => null
    "seller" => 1
    "buyer" => 3
  ]
  4 => array:7 [▼
    "id" => 3
    "name" => "Sample Listing 3"
    "quantity" => 1
    "price" => 50.5
    "photo" => null
    "seller" => 4
    "buyer" => 3
  ]
  2 => array:7 [▼
    "id" => 4
    "name" => "Sample Listing 4"
    "quantity" => 1
    "price" => "30.50"
    "photo" => null
    "seller" => 2
    "buyer" => 3
  ]
]

I have already tried solutions such as the following: Sum of subarray values , php array group , http://sandbox.onlinephpfunctions.com/code/3db42a4e13464eb027d85549f897a313fb8250eb

This result is produced from the following code:

        ($arr = session()->get('cart'));

        $newarray = array();
        foreach($arr as $ar) {

                foreach ($ar as $k => $v) {

                    if (array_key_exists($v, $newarray))
                        $newarray[$v]['price'] = $newarray[$v]['price']  + $ar['price'] ;
                    else if ($k == 'seller')
                        $newarray[$v] = $ar;
                }
            }

What I would like to do is: - Alter the price for each item that has quantity>1. - At the same time, group all the items that come from the same seller.

(so more than one identical items are stored in the array). I thought that it would be possible to just do the following:

if($ar['quantity'] > 1){
                        $newarray[$v]['price'] = $newarray[$v]['price'] * $ar['quantity'] + $ar['price'] * $ar['quantity'];
}

And the required result for items that have quantity > 1 would be:

array:3 [▼
  1 => array:7 [▼
    "id" => 1
    "name" => "Listing Sample 1"
    "quantity" => "1"
    "price" => 145.5
    "photo" => null
    "seller" => 1
    "buyer" => 3
  ]
  4 => array:7 [▼
    "id" => 3
    "name" => "Sample Listing 3"
    "quantity" => 3
    "price" => 151.5
    "photo" => null
    "seller" => 4
    "buyer" => 3
  ]
  2 => array:7 [▼
    "id" => 4
    "name" => "Sample Listing 4"
    "quantity" => 2
    "price" => "61"
    "photo" => null
    "seller" => 2
    "buyer" => 3
  ]
]

I think a valid solution is the one proposed from an example found in PHPSandbox by @mickmackusa http://sandbox.onlinephpfunctions.com/code/3db42a4e13464eb027d85549f897a313fb8250eb

George G
  • 93
  • 7
  • What is your expected output ? – Rakesh Jakhar Jul 11 '19 at 07:32
  • I edited the question and provided a possible output. – George G Jul 11 '19 at 07:34
  • @GeorgeG How were the quantities changed between your arrays? Are you attempting to group specific values? If so, please provide an accurate example data set and explanation on how the values should be grouped. – Will B. Jul 11 '19 at 07:48
  • @fyrye I will try to rephrase. Thank you. What my final goal is to have the price for all the items that have the same seller and their final price based on their quantity. – George G Jul 11 '19 at 07:49
  • what will yo do when same seller has more than one different item? because from your code i think you are only storing price for seller not product. – sultania23 Jul 11 '19 at 07:57
  • You should store price sellerwise and product wise and then add price per product – sultania23 Jul 11 '19 at 07:58
  • So should I create a new array for multiplying each item with its quantity? – George G Jul 11 '19 at 09:36
  • @mickmackusa Has the code for this, but it works as a function. http://sandbox.onlinephpfunctions.com/code/3db42a4e13464eb027d85549f897a313fb8250eb – George G Jul 11 '19 at 11:22

0 Answers0