Rob
Rob

Reputation: 6380

Copy data from certain array positions and adding them to the start of the array

I have the following array, I want to take the data from positions 1 & 2 and add them into the array at position 0, but only if the id is unique. I'd also like to set the quantities of the copied items to 0.

$arr = [
    [
        [
            'id' => 39235995,
            'quantity' => 1,
            'price' => 2.81
        ],
        [
            'id' => 39235995,
            'quantity' => 1,
            'price' => 2.81
        ]
    ],
    [
        [
            'id' => 39235995,
            'quantity' => 1,
            'price' => 2.81
        ],
        [
            'id' => 39236029,
            'quantity' => 1,
            'price' => 2.952
        ]
    ],
    [
        [
            'id' => 39236015,
            'quantity' => 1,
            'price' => 3.333
        ]
    ]
];

This was my attempt. It's copied the items and set their quantities to zero but hasn't taken into account the unique id's and has also added them to the end of the array.

// 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'],
      );
  }
}

// Divide up into the correct amount of parcels
$parcel_size = 2;
$parcels = array_chunk($resultArr, $parcel_size);


// Add all products into the first array so it's reduces quantities down in the original order
$new_orders = array_slice($parcels, 1);
foreach($new_orders as $order){
  $parcels[] = array(
    'id' => $order['id'],
    'quantity' => 0,
    'price' => $order['price'],
  );
}

This is what I'd like to create, where am I going wrong?

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

            [1] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

            [2] => Array
                (
                    [id] => 39236029
                    [quantity] => 0
                    [price] => 2.952
                ) 

            [3] => Array
                (
                    [id] => 39236015
                    [quantity] => 0
                    [price] => 3.333
                )                               

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

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

        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 39236015
                    [quantity] => 1
                    [price] => 3.333
                )

        )

)

The array created based on @Terminator-Barbapapa's answer:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

            [1] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

            [2] => Array
                (
                    [id] => 39236015
                    [quantity] => 0
                    [price] => 3.333
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

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

        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 39236015
                    [quantity] => 1
                    [price] => 3.333
                )

        )

)

Upvotes: 1

Views: 59

Answers (1)

Terminator-Barbapapa
Terminator-Barbapapa

Reputation: 3126

You can run through your first order collecting the ids of the parcels in it and then copy the parcels with an id that don't match any of the collected ids with the following code:

$ids = array();
foreach ( $new_orders as $key => $order ) {

    //Collect ids from first order  
    if ( $key == 0 ) {
        foreach ( $order as $parcel ) {
            array_push( $ids, $parcel['id'] );
        }

    //Copy parcels to first order
    } else {
        foreach ( $order as $parcel ) {
            if ( !in_array( $parcel['id'], $ids ) ) {
                $parcel['quantity'] = 0;
                array_push( $new_orders[0], $parcel );
            }
        }
    }
}

Before copying the parcel the quantity will be set to zero.

Array used as input:

$new_orders = array(
    array(
        array(
            'id' => 39235995,
            'quantity' => 1,
            'price' => 2.81,
        ),
        array(
            'id' => 39235995,
            'quantity' => 1,
            'price' => 2.81,
        ),
    ),

    array(
        array(
        'id' => 39235995,
        'quantity' => 1,
        'price' => 2.81,
        ),
        array(
        'id' => 39236029,
        'quantity' => 1,
        'price' => 2.952,
        )
    ),

    array(
        array(
        'id' => 39236015,
        'quantity' => 1,
        'price' => 3.333,
        ),
    ),
);

Upvotes: 2

Related Questions