0

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/>";
}
?>
4
Contributors
6
Replies
60
Views
9 Months
Discussion Span
Last Post by fazal_1
0

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];
    }
    ?>
0

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
]

Edited by pty

0

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];
}

Edited by fazal_1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.