0

I have these arrays in the beggining:

Array
(
    [products] => Array
        (
            [0] => 5
            [1] => 4
            [2] => 6
            [3] => 8
            [4] => 4
        )

    [qtys] => Array
        (
            [0] => 20
            [1] => 22
            [2] => 100
            [3] => 0
            [4] => 0
        )

)

I when to combine these arrays in order to have this :

Array
(
    [0] => Array
        (
            [product_id] => 5
            [qty] => 20
        )

    [1] => Array
        (
            [product_id] => 20
            [qty] => 22
        ) ...

But with this foreach :

$products = $post['products'];
            $qtys = $post['qtys'];

            $array = [];


            foreach($products as $product){
                foreach($qtys as $quantity){

                    if(!in_array($product, $array)){
                    $array[] = array(
                            'product_id' => $product,
                            'qty' => $quantity
                        );
                    }
                }
            }

            echo "<pre>".print_r($array, true)."</pre>";

I have this result :

Array
(
    [0] => Array
        (
            [product_id] => 5
            [qty] => 20
        )

    [1] => Array
        (
            [product_id] => 5
            [qty] => 22
        )

I tried many this, for example with break, continue. I even tried array_combine, the result wasn't what I expected. I thought about using array_unique, but is not working with multidimensional arrays (that's what I understood).

3 Answers3

1

You can keep your original $post array and use one simple foreach using the key:

foreach($post['products'] as $key => $value){
    $array[] = array('product_id' => $value,
                     'qty' => $post['qtys'][$key]
               );
}

If you want to avoid duplicate products as it seems from your !in_array, then just key the result by the product_id:

foreach($post['products'] as $key => $value){
    $array[$value] = array('product_id' => $value,
                           'qty' => $post['qtys'][$key]
                     );
}

If needed you can re-index with $array = array_values($array);.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

As your keys are numerical and continous starting from 0:

// $post is your array with all the data
$newArray = [];
for ($i=0; $i < count($post['products']); $i++) { 
    $newArray[] = [
        'product_id' => $post['products'][$i];
        'qty' => $post['qtys'][$i];
    ];
}

Also I'm assuming that the ´products´ and ´qtys´ arrays are the same length.

monstercode
  • 944
  • 6
  • 25
0

The problem is that you are using two nested loops, so all the items in your first array will be paired with all items in your second array. Instead you will need to loop by index and add items.

function mergeProductQuantities($products, $quantities) {
    //Creating an empty output array
    $output = array();
    //Getting the number of iterations
    $limit = ($pc = count($products)) > ($qc = count($quantities)) ? $pc : $qc;
    //Doing the actual iteration
    for ($index = 0; $index < $limit; $index++) {
        //Creating a new item
        $newItem = array();
        //Setting applicable values
        if (isset($products[$index])) $newItem["product_id"] = $products[$index];
        if (isset($quantities[$index])) $newItem["qty"] = $quantities[$index];
        //Adding the new item to the output
        $output[]=$newItem;
    }
    return $output;
}

And call it like this:

$result = mergeProductQuantities($post['products'], $post['qtys']);
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175