The essence is this, I extract a multidimensional array from the database, then I check for the presence of $ users ['tt'] ! In_array ($ i, $ users) , if there is such an id ($ users ['tt']) then I insert data if I don’t insert another block class = "number" with the current number $ i Ps.I'm sorry for the clumsy description.

//Массив взят через print_r $count_div = 50; //Сколько должно быть 50 блоков (Данные тоже берутся из бд) $users = Array ( [0] => Array ( [id] => 1 [gid] => 36 [tt] => 7 [user] => 2 [wr] => 0 [timestamp] => 0 [login] => demo [email] => demo@demo.dd ) [1] => Array ( [id] => 2 [gid] => 36 [tt] => 2 [user] => 3 [wr] => 0 [timestamp] => 0 [login] => demo1 [email] => demo@no.mail ) ) for( $i = 1; $i <= $count_div; ++$i ) { if( !in_array($i, $users) ){ $users[] = $i; echo '<div class="box"><div class="number">'.$i.'</div></div>'; }else{ echo '<div id="'.$user['tt'].'" class="box"><img src="'.$user['email'].'.jpg" alt="User '.$user['user'].'"></div>'; } } 
  • And what's wrong here? - Byulent
  • in_array will not work, because in $ users only arrays, without values. A function with nested arrays does not work. Search function for nested arrays - here stackoverflow.com/questions/4128323/… - Alexey Shatrov
  • Thanks for the answer, I’m going to dig in that direction) - Andrei Andrei
  • @Andrei Andrei, Try using the array_filter function. - And

1 answer 1

 $count_div = 50; //Сколько должно быть 50 блоков (Данные тоже берутся из бд) $users = Array ( [0] => Array ( [id] => 1 [gid] => 36 [tt] => 7 [user] => 2 [wr] => 0 [timestamp] => 0 [login] => demo [email] => demo@demo.dd ) [1] => Array ( [id] => 2 [gid] => 36 [tt] => 2 [user] => 3 [wr] => 0 [timestamp] => 0 [login] => demo1 [email] => demo@no.mail ) ) for( $i = 1; $i <= $count_div; ++$i ) { if( !in_array_r($i, $users) ){ $users[] = $i; echo '<div class="box"><div class="number">'.$i.'</div></div>'; }else{ echo '<div id="'.$user['tt'].'" class="box"><img src="'.$user['email'].'.jpg" alt="User '.$user['user'].'"></div>'; } } function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; }