0

I'm attempting to create a multidimensional array which should have the ID and quantity from the $_POST array. At the moment it seems to put every quantity into each an element with each ID.However I want it to take the first elements from each array and then add them together to a new array and so on.

Whereas it should be

ID 1 - Quantity 100
ID 2 - Quantity 50

etc

But at the moment I get this

array(16) {
  [0]=>
  array(2) {
    ["id"]=>
    string(1) "1"
    ["quantity"]=>
    string(2) "50"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(1) "1"
    ["quantity"]=>
    string(3) "100"
  }
  [2]=>
  array(2) {
    ["id"]=>
    string(1) "1"
    ["quantity"]=>
    string(3) "100"
  }
  [3]=>
  array(2) {
    ["id"]=>
    string(1) "1"
    ["quantity"]=>
    string(3) "100"
  }
  [4]=>
  array(2) {
    ["id"]=>
    string(2) "12"
    ["quantity"]=>
    string(2) "50"
  }
  [5]=>
  array(2) {
    ["id"]=>
    string(2) "12"
    ["quantity"]=>
    string(3) "100"
  }
  [6]=>
  array(2) {
    ["id"]=>
    string(2) "12"
    ["quantity"]=>
    string(3) "100"
  }
  [7]=>
  array(2) {
    ["id"]=>
    string(2) "12"
    ["quantity"]=>
    string(3) "100"
  }
  [8]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["quantity"]=>
    string(2) "50"
  }
  [9]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["quantity"]=>
    string(3) "100"
  }
  [10]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["quantity"]=>
    string(3) "100"
  }
  [11]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["quantity"]=>
    string(3) "100"
  }
  [12]=>
  array(2) {
    ["id"]=>
    string(1) "6"
    ["quantity"]=>
    string(2) "50"
  }
  [13]=>
  array(2) {
    ["id"]=>
    string(1) "6"
    ["quantity"]=>
    string(3) "100"
  }
  [14]=>
  array(2) {
    ["id"]=>
    string(1) "6"
    ["quantity"]=>
    string(3) "100"
  }
  [15]=>
  array(2) {
    ["id"]=>
    string(1) "6"
    ["quantity"]=>
    string(3) "100"
  }
}

Here is my PHP code.

foreach($_POST['sweetids'] as $id) {

foreach($_POST['quantites'] as $quantity) {

    $stock_array[] = array(
        "id"=> $id,
        "quantity" => $quantity
        );
}

}

4
  • Can you show what the desired output is? Commented Feb 19, 2016 at 14:43
  • From what I see, your array is working as it should. What are you trying to get? Commented Feb 19, 2016 at 14:57
  • Can you also show the POST data? Commented Feb 19, 2016 at 15:07
  • You're iterating $_POST['quantities'] multiple times. I doubt that's what you intended. Commented Feb 19, 2016 at 16:23

2 Answers 2

1

I think this is what you're trying to achieve:

foreach($_POST['sweetids'] as $key=>$id) {

    $stock_array[] = array(
        "id"=> $id,
        "quantity" => $_POST['quantities'][$key]
        );
}
Sign up to request clarification or add additional context in comments.

Comments

0

You're iterating $_POST['quantities'] for every $_POST['sweetids'] which is probably not what you intend. When you iterate both, your result will be every combination of sweetids and quantities, not each pair of them.

I'm guessing you meant something more like:

// Assuming you already verified that $_POST['quantities'] and $_POST['sweetids'] exist
//   and that both of them have the same number of elements
for ( $i = 0, $len = count($_POST['sweetids']); $i < $len; $i++ ) {
    $stock_array[] = array(
        'id' => $_POST['sweetids'][$i],
        'quantity' => $_POST['quantities'][$i]
    );
}

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.