i have an array $value=array (2,4,6,3,2,4,3,8);
and i need only the values thar are repeating it self in the array
that would be
2,4,3
i have this code written

<?php
$value=array (2,4,6,3,2,4,3,8);
$arrlength = count($value);
for($x=0; $x<$arrlength; $x++){
     $value[$x];
     for($y=0; $y<$arrlength; $y++){
         $value[$y];
         if($value[$x]==$value[$y] ){
             echo $value[$y];
         }  
     }
     echo "<br/>";
}
?>

Recommended Answers

All 6 Replies

now im getting this out put 243243 but i only need 243 can any one help here is my updated code and i also have saved the values im getting in new array

<?php
    $value=array (2,4,6,3,2,4,3,8);
    $arrlength = count($value);
    $secndarr=array();
    for($x=0; $x<$arrlength; $x++){
         $value[$x];
         for($y=0; $y<$arrlength; $y++){
             $value[$y];
             if($value[$x]==$value[$y] && $x!=$y ){
                 echo $value[$y];
                 $secndarr[]=$value[$y]; 
                ;
                 echo "<br/>"; 
             }
         }
    }
    $arrlengthsec = count($secndarr);
    for($z=0; $z<$arrlengthsec ; $z++){
        echo $secndarr[$z];
    }
    ?>
Member Avatar for diafol

Oit of curiosity why can.t you use a built in function?

just trying to built my logics

Classic homework

commented: Did cross my mind :) +15

I was going to help, but rather than just help you in the way that you want (that'd be too boring) I thought I'd give the answer in Ruby.

[2,4,6,3,2,4,3,8]
  .group_by{|i| i}
  .select{|_,matches| matches.count > 1}
  .keys

=> [
    [0] 2,
    [1] 4,
    [2] 3
]

its not a home work just trying to Learn
if any one have a good logic Question like that for me let me know
im a starter and learing
what should i try next and the previous task is compleated tanks to all you guys
here the code just need to add value of x in the 2nd loop

$value=array (2,4,6,3,2,4,3,8);
$arrlength = count($value);
$secndarr=array();
for($x=0; $x<$arrlength; $x++){
     $value[$x];

     for($y=$x; $y<$arrlength; $y++){
         $value[$y];
         if($value[$x]==$value[$y] && $x!=$y ){
             echo $value[$y];
             $secndarr[]=$value[$y]; 
            ;
             echo "<br/>"; 
         }
     }
}
$arrlengthsec = count($secndarr);
for($z=0; $z<$arrlengthsec ; $z++){
    echo $secndarr[$z];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.