Answer the question
In order to leave comments, you need to log in
Why doesn't the code add all the elements to the array?
Hello, I accept an array of data via POST
Array
(
[type-sale] => Купля
[photo1] => https://i.mycdn.me/image?id=850624141910&t=3&plc=WEB&tkn=*Vn9W27REGyHaVAYkwX6XoqZjQs4
[photo2] => https://i.mycdn.me/image?id=850624141910&t=3&plc=WEB&tkn=*Vn9W27REGyHaVAYkwX6XoqZjQs4
[photo3] => https://i.mycdn.me/image?id=850624141910&t=3&plc=WEB&tkn=*Vn9W27REGyHaVAYkwX6XoqZjQs4
[photo4] => https://i.mycdn.me/image?id=850624141910&t=3&plc=WEB&tkn=*Vn9W27REGyHaVAYkwX6XoqZjQs4
[photo5] =>
[photo6] => https://i.mycdn.me/image?id=850624141910&t=3&plc=WEB&tkn=*Vn9W27REGyHaVAYkwX6XoqZjQs4
[photo7] =>
)
$nmPhoto = array('photo1','photo2','photo3','photo4','photo5','photo6','photo7');
for ($i=0;$i<=6;$i++) {
$nextName = $nmPhoto[$i];
if ($_POST[$nextName] == null) {
break 1;
}
else {
$photo [$i] =$_POST[$nextName] ;
}
}
//photo end
echo "<pre>";
print_r ($photo);
echo "</pre>";
Array
(
[0] => https://i.mycdn.me/image?id=850624141910&t=3&plc=WEB&tkn=*Vn9W27REGyHaVAYkwX6XoqZjQs4
[1] => https://i.mycdn.me/image?id=850624141910&t=3&plc=WEB&tkn=*Vn9W27REGyHaVAYkwX6XoqZjQs4
[2] => https://i.mycdn.me/image?id=850624141910&t=3&plc=WEB&tkn=*Vn9W27REGyHaVAYkwX6XoqZjQs4
[3] => https://i.mycdn.me/image?id=850624141910&t=3&plc=WEB&tkn=*Vn9W27REGyHaVAYkwX6XoqZjQs4
)
Answer the question
In order to leave comments, you need to log in
break breaks the loop. why do you even need to check the ==null condition?
$nmPhoto = array('photo1','photo2','photo3','photo4','photo5','photo6','photo7');
$resultPhoto = [];
foreach ($nmPhoto as $key => $name) {
if (isset($_POST[$name])) {
$resultPhoto[$key] = $_POST[$name];
}
}
echo '<pre>';
print_r ($resultPhoto);
echo '</pre>';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question