Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Filtering array problem

    I've been looping arround this resultset trying to find a way to filter this and nothing. Anyone available to give me a hand here? Thanks in advance.

    Here's what's up:
    These are the results I have on $foundStyles but the ones in red shouldn't go to the query. Is there a way to filter these results?

    KEY: 0 & VALUE: 3488
    KEY: 1 & VALUE: 3485
    KEY: 2 & VALUE: 2860
    KEY: 3 & VALUE: 3482
    KEY: 4 & VALUE: 3
    KEY: 5 & VALUE: 0
    KEY: 6 & VALUE: 1
    KEY: 7 & VALUE: 2
    KEY: 8 & VALUE: 3
    KEY: 9 & VALUE: 4


    In fact, I think that my problem comes a little from above where I have the following code to obtain ONLY the values in the array that have more than one occurence.

    PHP Code:

    // The array $foundStyles would seem something like this
    $foundstyles=array(0=>34881=>34852=>34883=>25624=>34855=>17856=>2232);

    // I just want the 3488 and 3485 since these are the only ones that are repeated
    // and what I need is to make the array $foundstyles to become like this
    // array(0=>3488, 1=>3485) and for that I made the code below

    // GET ONLY THE KEYS DUPLICATES FOR NEXT QUERY
    if($rows>0){ // From mysql_query
        
    $foundStyles=array_count_values($foundStyles);
    }
    foreach(
    $foundStyles as $key=>$value){
        if(
    $value>=$total_styles){
            
    $foundStyles[]=$key;
        }

    Humm....
    Last edited by MarioPro; 12-29-2005 at 01:03 PM.

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    330
    Thanks
    0
    Thanked 13 Times in 13 Posts
    I'm slightly confused about what you want, if you want to find the duplicates in an array, you could use this:
    PHP Code:
    function duplicates($array)
        {
            
    $dupes = array();
            
    $num array_count_values($array);
            foreach(
    $num as $k => $v)
                {
                    if(
    $v >= 2)
                        {
                            
    $dupes[] = $k;
                        }
                }
            return 
    $dupes;
        }

    // test it:    
    $test = array(1,1,2,3,4,4,5,5,5,6,7,7,7,7);
    header('Content-Type: text/plain');
    print_r(duplicates($test));
    /* Outputs:
    Array
    (
        [0] => 1
        [1] => 4
        [2] => 5
        [3] => 7
    )*/ 
    The problem from your original code, might be that you are appending to the array that already had the array_count_values() in it, and where does $total_styles come from?

    Edit: spelling.
    Last edited by schleppel; 12-29-2005 at 01:40 PM.

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks schleppel,

    I was browsing and browsing the php website trying to find examples podted by users and as I was just getting to some place you posted this.
    Yes, this is what I need, because I'm quering a database in a foreach loop that will return an array with values that may or not be duplicated. Since I just want the ones that are duplicates (the oposite from array_unique) this function will do the job (I hope but believe it will).
    Let me test it and I'll post here the feedback. Meanwhile, thanks for your help.

    Opss. Answering your question about $total_styles. $total_styles is the variable containing the total $styles[] array for wich I have to loop the db query.

    PHP Code:

    $total_styles
    =count($styleID);
    foreach(
    $styleID as $k=>$v){
        
    // SEARCH
        
    $query="SELECT * FROM pstyles_rel ";
        
    $query.=" WHERE style_ID='$v' ";
        
    $result=mysql_query($query) or die("Can't select styles: ".mysql_error());
        
    $rows=mysql_num_rows($result);
        if(
    $rows>0){
            while(
    $fields=mysql_fetch_array($result)){
                
    $foundStyles[]=$fields["p_ID"]; // the 3488 and so on values I need
            
    }
        }

    Last edited by MarioPro; 12-29-2005 at 01:54 PM.

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, I forgot a little issue, is that I want in fact only the one that are duplicate but if the array contains more than one value, otherwise I will miss a result.

    in this I would apply your function:
    $test = array(1,1,2,3,4,4,5,5,5,6,7,7,7,7);
    header('Content-Type: text/plain');
    print_r(duplicates($test));
    /* Outputs:
    Array
    (
    [0] => 1
    [1] => 4
    [2] => 5
    [3] => 7
    )

    but in this I would miss it
    $test = array(7);

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    330
    Thanks
    0
    Thanked 13 Times in 13 Posts
    So if the array only has one value, you don't want to test for duplicates?

    PHP Code:
    if(count($array) > 1)
        {
            
    // test for duplicates
        
    }
    else
        {
            
    // don't test for duplicates
        

    Or are you asking for something else?

  • #6
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Forgive-me.

    I only whant to check for duplicates if the $total_styles > 1, this is to say that if $total_styles==1 than I will only loop 1 time on the query and thus no duplicates will be found.

    So I think I'm right if I do it like

    PHP Code:

    if($total_styles>&& count($array)>$total_styles
        { 
            
    // test for duplicates 
        

    else 
        { 
            
    // don't test for duplicates 
        



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •