Rob
Rob

Reputation: 6380

Splitting the quantity of an item into separate arrays if over 1

I'm trying to split up the quantities of an item so each array has a quantity of 1. So if an item had the quantity 3, then it would output 3 array's each with the quantity set to 1.

I have the following code with the output from the array:

// Add the product info we need into a new array
$total_products = array();
foreach($products as $product){

  $product_id = $product['sellable']['id'];
  $product_quantity = $product['quantity'];
  $product_price = $product['price_per_unit'];

  $total_products[] = array('id' => $product_id, 'quantity' => $product_quantity, 'price' => $product_price);
}

echo "<pre>";
print_r($total_products);
echo "</pre>";

Array
(
    [0] => Array
        (
            [id] => 39236029
            [quantity] => 3
            [price] => 2.953333
        )

    [1] => Array
        (
            [id] => 39236017
            [quantity] => 1
            [price] => 3.14
        )

)

I then have the following code:

// Separate out the quantities for each product, one per array
$resultArr = [];
foreach($total_products as $item){
  for($i = 0; $i < $item['quantity']; $i++){
      $resultArr[] = array(
        'id' => $item['id'],
        'quantity' => 1,
        'price' => $item['price'],
      );
  }
}

I want to run through the array above and create a new array where anything that has a quantity more than 1 needs to be split into separate arrays. So the first item that had a quantity of 3, has now been split into 3 arrays each with a quantity of 1.

Array
(
    [0] => Array
        (
            [id] => 39236029
            [quantity] => 1
            [price] => 2.953333
        )

    [1] => Array
        (
            [id] => 39236029
            [quantity] => 1
            [price] => 2.953333
        )

    [2] => Array
        (
            [id] => 39236029
            [quantity] => 1
            [price] => 2.953333
        )            

    [3] => Array
        (
            [id] => 39236017
            [quantity] => 1
            [price] => 3.14
        )

)

At the moment, it's creating the following, where am I going wrong?

Array
(
    [0] => Array
        (
            [id] => 39236029
            [quantity] => 1
            [price] => 2.953333
        )

    [1] => Array
        (
            [id] => 39236029
            [quantity] => 1
            [price] => 2.953333
        )            

    [2] => Array
        (
            [id] => 39236017
            [quantity] => 1
            [price] => 3.14
        )

)

Upvotes: 0

Views: 134

Answers (1)

Cameron Hurd
Cameron Hurd

Reputation: 5031

There's gotta be something missing between the code in your execution env vs. what's in the question!

Here's what I fed into 3v4l:

(Only the lightest assumptions/inferences about your $products var.)

<?php

// Add the product info we need into a new array
$total_products = [];

$products = [
    [
        'sellable' => [ 'id' => 39236029 ],
        'quantity' => 3,
        'price_per_unit' => 2.953333
    ],
    [
        'sellable' => [ 'id' => 39236017 ],
        'quantity' => 1,
        'price_per_unit' => 3.14
    ]
];


foreach($products as $product){
  $product_id = $product['sellable']['id'];
  $product_quantity = $product['quantity'];
  $product_price = $product['price_per_unit'];

  $total_products[] = array('id' => $product_id, 'quantity' => $product_quantity, 'price' => $product_price);
}

// Separate out the quantities for each product, one per array
$resultArr = [];

foreach($total_products as $item){
  for($i = 0; $i < $item['quantity']; $i++){
      $resultArr[] = array(
        'id' => $item['id'],
        'quantity' => 1,
        'price' => $item['price'],
      );
  }
}

echo "<pre>";
print_r($resultArr);
echo "</pre>";

And here's the output - looks like what you're hoping for!

<pre>Array
(
    [0] => Array
        (
            [id] => 39236029
            [quantity] => 1
            [price] => 2.953333
        )

    [1] => Array
        (
            [id] => 39236029
            [quantity] => 1
            [price] => 2.953333
        )

    [2] => Array
        (
            [id] => 39236029
            [quantity] => 1
            [price] => 2.953333
        )

    [3] => Array
        (
            [id] => 39236017
            [quantity] => 1
            [price] => 3.14
        )

)
</pre>

Edit: just for yucks I did the "execute EOL versions" option and confirmed this output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.21, 7.4.0 - 7.4.9, 8.0.0alpha1 - beta1 !

Upvotes: 1

Related Questions