J
J
Jony13372017-02-04 11:35:14
PHP
Jony1337, 2017-02-04 11:35:14

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] => 
)

Sometimes only 3 or 4 photos can come, sometimes 7 and I decided to stir up a codec that will check the data that comes through the Post
here is my code
$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>";

on output
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
)

it's only the first 4 links, i.e. it gets to the space and doesn't execute further or what?
May the force be with you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WQP, 2017-02-04
@WQP

For arrays use php.net/manual/ru/control-structures.foreach.php

C
catrinblaidd, 2017-02-04
@catrinblaidd

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>';

PS and strength, after all, will be)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question