I want to display number which are repeated. $arr = array('1', '2', '1', '3', '4', '2', '7', '5', '6', '7', '1', '8', '9'); here 1, 2, 7 are repeated and i want to echo only these 3 element which are repeated. result like this : 1 2 7 without any php function using only loop(for, foreach, while, etc.) plz help.................. thanks...................
Try this: $arr = array('1', '2', '1', '3', '4', '2', '7', '5', '6', '7', '1', '8', '9'); $result=array_unique(array_diff_assoc($arr,array_unique($arr))); foreach($result as $result1) { echo $result1, '<br>'; } PHP:
thanks for reply.................. but my condition is that you can't use these function array_unique_and_array_diff_assoc without using these function only use loop or some thing other........... thanks..........
You can use the basic code as below <?php $arr = array('1', '2', '1', '3', '4', '2', '7', '5', '6', '7', '1', '8', '9'); $tempArr = array(); foreach($arr as $eachElement) { if(!in_array($eachElement, $tempArr)) { echo $eachElement."<br/>"; $tempArr[] = $eachElement; } } ?>
^ The above will print 1271, and it uses in_array Here is my solution: $arr = array('1', '2', '1', '3', '4', '2', '7', '5', '6', '7', '1', '8', '9'); $tempArr = array(); $uniques = array(); foreach($arr as $eachElement){ if (in_php_arr($eachElement, $tempArr) && !in_php_arr($eachElement, $uniques)){ $uniques[] = $eachElement ; } else { $tempArr[] = $eachElement ; } } foreach ($uniques as $e) { echo $e ; } function in_php_arr($val , $tempArr){ foreach ($tempArr as $temp) { if ($temp ==$val ){ return true ; } } return false ; } PHP: Although I am sure there is a simpler solution somewhere..