简体   繁体   中英

Creating multidimensional array PHP

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
        );
}

}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM