It is necessary to deduce the elements 2, 5, 3, 10

What to finish in my code, what would he bring these elements?

<?php $arr = array(2, 5, 5, 3 ,2, 10); for ($i=0; $i<count($arr); $i++ ) { for ($j=0; $j < $i; $j++ ) { if($arr[$i]==$arr[$j]) echo $arr[$i]. " "; } } ?> 

Now displays only 2, 5. It is necessary that 3 more, 10 output.

  • one
    and what is wrong now? what is it now? What are you doing in this code? why do you do it according to your plan? at least some explanations are possible? ........ I just wonder: you yourself understand what you wrote? - Alexey Shimansky
  • Well, I wrote such logic that the script will only output repeating elements in the array in one number. And I need the rest to be output too - Beginner

3 answers 3

It should work

 <?php $arr = array(2, 5, 5, 3 ,2, 10); $arr_copy = array(); for ($i = 0; $i < count($arr); $i++ ) { $ret = 1; for($j = 0; $j < count($arr_copy); $j++){ if($arr[$i] == $arr_copy[$j]){ $ret = 0; } } if($ret == 1){ $arr_copy[count($arr_copy)] = $arr[$i]; echo $arr[$i]. " "; } } ?> 
  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash

The easiest way to the forehead

 $inputArray = array(2, 5, 5, 3 ,2, 10); $outputArray = array(); // вся работа происходит тут foreach($inputArray as $inputArrayItem) { foreach($outputArray as $outputArrayItem) { if($inputArrayItem == $outputArrayItem) { continue 2; } } $outputArray[] = $inputArrayItem; } // это уже вывод итогового результата foreach($outputArray as $output) { echo $output.'<br>'; } 

from the docks :

continue takes an optional numeric argument that indicates how many levels of nested loops the rest of the iteration will be skipped. The default value is 1, which skips the rest of the current loop.


There is another working method that I looked at in some place, but which I myself do not understand how it works))

 $array = array(2, 5, 5, 3 ,2, 10); $unique = array(); foreach($array as $v) isset($k[$v]) || ($k[$v] = 1) && $unique[] = $v; // результат echo '<pre>'; print_r($unique); echo '</pre>'; 
  • Heh, not bad :) - Yuri
  • continue 2; - How to understand it? - Beginner
  • @Sven php.net/manual/ru/control-structures.continue.php continue принимает необязательный числовой аргумент, который указывает на скольких уровнях вложенных циклов будет пропущена оставшаяся часть итерации. Значением по умолчанию является 1, при которой пропускается оставшаяся часть текущего цикла. continue принимает необязательный числовой аргумент, который указывает на скольких уровнях вложенных циклов будет пропущена оставшаяся часть итерации. Значением по умолчанию является 1, при которой пропускается оставшаяся часть текущего цикла. and there is an example already with continue 3 - Aleksey Shimansky
  • ok thanks for the help! - Beginner

See how I still remade

 $arr = array(2, 5, 5, 3 ,2, 10, 10, 0); for ($i = 0; $i < count($arr); $i++ ) { $ret = 1; for($j = 0; $j < $i; $j++){ if($arr[$i] == $arr[$j]){ $ret = 0; } } if($ret == 1){ echo $arr[$i]. " "; } } 

You can also like that

  • Yuri, Alexey, advise pliz, has the same right to life? Just like that, the minimal code came out for me and the logic seems to be working - Beginner
  • Yes, this solution has) I checked - the code is working - Yuri