Good day.
When iterating through an array, the variable "wraps" back. Why?

Code:

foreach ($_POST as $key=>$value) { if ($key == "username") {$username = $value;} elseif ($key == "phone") {$phone = $value;} elseif ($key == "email") {$email = $value;} elseif ($key == "shiptype") {$shiptype = $value;} elseif ($key == "cardtype") {$cardtype = $value;} elseif ($key == "description") {$description = $value;} else { $id = $key; $volume = $value; } $zakaz .= 'Товар: '.$id.', количество — '.$volume.'<br />'; } echo 'Имя: '.$username.'<br />Телефон: '.$phone.'<br />Почта: '.$email.'<br />Тип доставки: '.$shiptype.'<br />Метро: '.$cardtype.'<br />Примечание: '.$description.'<br />'; echo $zakaz; 

In $ _POST, I get an unknown amount of $ key => $ value, so first we screen out the known $ key values, and the remaining ones need to be listed at the end. But it turns out at the output:

 Имя: %user% Телефон: %phone% Почта: %mail% Тип доставки: %mail% Метро: %metro% Примечание: %additional% Товар: 1, количество — 1000 Товар: 8, количество — 5 Товар: 9, количество — 4 Товар: 9, количество — 4 Товар: 9, количество — 4 Товар: 9, количество — 4 Товар: 9, количество — 4 Товар: 9, количество — 4 Товар: 9, количество — 4 

Although the user has selected item 9 once, he is listed 7 times. Chyadt?

    2 answers 2

    So in each iteration of foreach you add a certain string to $ zakaz. Decision:

     foreach ($_POST as $key=>$value) { if ($key == "username") {$username = $value;} elseif ($key == "phone") {$phone = $value;} elseif ($key == "email") {$email = $value;} elseif ($key == "shiptype") {$shiptype = $value;} elseif ($key == "cardtype") {$cardtype = $value;} elseif ($key == "description") {$description = $value;} else { $id = $key; $volume = $value; $zakaz .= 'Товар: '.$id.', количество — '.$volume.'<br />'; } } 

    Although your approach is generally incorrect.

    My advice is to transfer purchases separately ( <input type="hidden" name="items[<?=$id?>]" value="<?=$quantity?>" /> ) + Something like that. Look at the result, should like (if I'm not messed up where).

     $order = new stdClass; $order->items = array(); foreach (array('username','phone','email','shiptype','cardtype','description') as $code) $order->$code = (string)@$_POST[$code]; foreach ($_POST['items'] as $id => $q) $order->items[$id] = $q; var_dump($order); 
    • one
      Now I will try your advice. PS purchases and other information I pass to ajax via serializeArray (). - Realetive__

    To begin, try to identify the reason why product number 9 comes to you 7 times. And from the code it is clear that such a number of lines with item 9 to you exactly comes in $_POST . That's where, as they say, the dog rummaged. And if you modify the working code in order to tune it under the joint, which arises for unexplained reasons, then this is traditionally called a “crutch”. Do not be so :) start with the main thing - why does he come to you 7 times? and to remove the goods in the items or not - it does not matter. This affects the beauty of the code, and not its performance. Your code is quite working, look for why it comes 7 times.