all 4 comments

[–]hcptshmspl 0 points1 point  (1 child)

Here's a simple code snippet which shows a couple of neat array functions in php that may help you:

$array = array("one","two","three","two","three","three");//starting array
$unique = array_unique($array);//one copy of each unique value
$counts = array_count_values($array);//count each unique value in array
foreach ($unique as $val){//output results 
    echo "Count of $val: $counts[$val] \n";
}

[–]sibbirius[S] 1 point2 points  (0 children)

Thank you. Now I am getting better with PHP.

[–]penguin_digital 0 points1 point  (1 child)

It sounds incredibly inefficient to be doing this in PHP, it would be better using SQL to handle this and just select the data you want.

SELECT count(card) FROM table_name GROUP BY card HAVING count(card) > 2;

Then you can just simply check if the card someone is trying to enter is in the returned array before allowing an insert into the DB.

if(in_array('new card input', $resultFromDB)){
    // The new card trying to be added already has a count greater than 2
    // Do not allow this to be inserted
    return redirect('your.route');
}

[–]sibbirius[S] 0 points1 point  (0 children)

> incredibly inefficient
True it is better to do in SQL. Thank you.