how to print only repeated number.....

Discussion in 'PHP' started by dineshsingh1984, Sep 8, 2014.

  1. #1
    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...................
     
    dineshsingh1984, Sep 8, 2014 IP
  2. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #2
    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:
     
    malky66, Sep 9, 2014 IP
  3. dineshsingh1984

    dineshsingh1984 Active Member

    Messages:
    154
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    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..........
     
    dineshsingh1984, Sep 9, 2014 IP
  4. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #4
    Why can't you use that function?
    You can't do it without using some sort of PHP functions.
     
    Last edited: Sep 9, 2014
    malky66, Sep 9, 2014 IP
  5. abhicyco

    abhicyco Active Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #5
    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;
    }
    }
    ?>
     
    abhicyco, Sep 9, 2014 IP
  6. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #6
    ^ 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..
     
    kutchbhi, Sep 9, 2014 IP
  7. dineshsingh1984

    dineshsingh1984 Active Member

    Messages:
    154
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    thanks for help
     
    dineshsingh1984, Sep 14, 2014 IP
  8. dineshsingh1984

    dineshsingh1984 Active Member

    Messages:
    154
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #8
    thanks for help..........
     
    dineshsingh1984, Sep 14, 2014 IP