Skip to content
Advertisement

avoid same numbers and zero in array

I have a form with that table: jsfiddle

In this form i need to enable to edit the number fields.
After submit i collect the data and write to mysql.

JavaScript

Before mysql insert i want to check that the $number array does not contain the same numbers and zero (like: 0,1,1,101,150).
If yes give an error to the user that the Number field need to be unique.
How can I do that the easiest way with php or javascript? thanx 😉

Advertisement

Answer

JavaScript

Output:

JavaScript

array_unique is going to give you an array that contains only the unique elements. That means, if an element has duplicates, count(array_unique($array)) is going to be smaller than count($array).

Check array_unique here: https://www.php.net/manual/en/function.array-unique.php

Edit: I suggest reading other people’s comments about improving your code, too.

Forgot for zero.

JavaScript

To check if an element is in an array (if array contains 0, for example) you can use in_array.

Check in_array here: https://www.php.net/manual/en/function.in-array.php

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement