Look for duplicate values in a associative array and add them to a count

后端 未结 2 1350
梦谈多话
梦谈多话 2021-01-22 01:17

Hi I am trying to count the number of duplicate values in a associative array that looks like this:

array(3) { [0]=> array(3) { [\"Title\"]=> string(25) \"         


        
相关标签:
2条回答
  • 2021-01-22 01:56

    If you want to detect dups in an array that you are creating, something like this avoid having to go through the array multiple times:

    $arr=array();
    while($row = mysql_fetch_array($result))  {                  
      $arr[$row['prodTitle']] = isset($arr[$row['prodTitle']]) 
                                   ? $arr[$row['prodTitle']] +1 
                                   : 0;                    
    }
    $dups = array_keys(array_filter($arr)); //any key => 0 will be filtred out
    

    If you want to just get the dups directly by SQL, have a look at this:

    Your current query--

    SELECT prodTitle 
      FROM product 
    WHERE prodTitle != '{$keyword}' 
      AND creditCard IN( SELECT creditCard FROM product WHERE prodTitle ='{$keyword}');
    

    which given data like this

    prod cc
    A    1
    A    2
    A    3
    A    1
    A    1
    B    15
    B    1
    B    2
    B    21
    C    10
    C    1
    

    returns this set (with $keyword=='A'):

    prod 
    B
    B
    C
    

    An aggregate query that returns only records where credit cards used on non-X were also used on X at least twice --

    SELECT p1.prodTitle, COUNT(p2.creditCard) AS numrecords
      FROM product p1
        JOIN product p2
        ON (p1.creditCard = p2.creditCard)
    WHERE p1.prodTitle != '{$keyword}'
      AND p2.prodTitle = '{$keyword}'
    GROUP BY p1.prodTitle 
      HAVING COUNT(p2.creditCard) > 1;
    

    given the same data, returns this set:

    prod  num
    B    2
    

    Doing an aggregate query avoids all the messing about with loops. Here's a link to the MySQL list of aggregate functions.

    0 讨论(0)
  • 2021-01-22 02:01

    Okey so I found my solution and I made it look something like this probably not he most efficient way but it works:

    $prodArray = array();
    $ar = array();
    
    
    $query ="THE QUERY";
    $result = mysql_query($query) or die(mysql_error());
    
    while($row = mysql_fetch_array($result))
    {
    array_push($ar, $row['prodTitle']);
    }
    
    function findDuplicates($data,$dupval) {
    $nb= 0;
    foreach($data as $key => $val)
    if ($val==$dupval) $nb++;
    return $nb;
    }
    
    
    $uniarr = array_unique($ar);
    
    //Will run the function findDuplicates for each unique title in the array
    for ($i = 0; $i < sizeof($uniarr); $i++)
    {
    $count = findDuplicates($ar, $uniarr[$i]);
    array_push($prodArray, array( Title => $uniarr[$i], Count => $count));
    }
    
    //Displays 2 of the results in the array
    for ($c = 0; $c < 2; $c++)
    {
    echo "The title:".$prodArray[$c]["Title"]." And the amount:".$prodArray[$c]["Count"];
    echo "<br />";
    }
    

    So that's pretty much it feel free to post your comments on this and if you have any improvements feel free to post them.

    0 讨论(0)
提交回复
热议问题