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
61
Views
1 Year
Discussion Span
Last Post by fazal_1

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

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

just trying to built my logics

Classic homework

Did cross my mind :)

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

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