Forum Moderators: coopster

Message Too Old, No Replies

How to check an array for redundancy?

Got one workaround method, looking for simpler

         

Artie_J

1:49 pm on Nov 19, 2005 (gmt 0)

10+ Year Member



I can't find a function that would check an array for redundancies!
Here's the situation:
On page A there is a form with several Select input fields, that all have the same group of option values.
The selected values get posted to page B as variables.
I need a way to check that eack selected value is unique, and there is no redundancy.
I can't use a single Multiple select field, because each value must have an associated quantity value that gets posted to page B also.
I though about this method:
$array1 = array($value2, $value3, $value4);
$array2 = array($value1, $value3, $value4);
----etc.----
then using in_array:
if (in_array($value1, $array1)) {
do something;
}
----etc.----
checking all arrays for the value that should be missing from that array if all values are unique.
If a redundancy is found, I'd like a warning that
says something like "valueX is selected more than once".

I guess this works, but does anyone know of a simpler method?

directrix

4:04 pm on Nov 19, 2005 (gmt 0)

10+ Year Member



The array_intersect [php.net] function may give you what you need. Otherwise, check out some of the other array functions.

Artie_J

4:34 pm on Nov 19, 2005 (gmt 0)

10+ Year Member



Thanks, and excuse my noobiness. :-) I now see the complete list of the array related functions at php.net, and that function might do the trick.
Thanks.

Artie_J

2:13 am on Nov 20, 2005 (gmt 0)

10+ Year Member



array_keys() can do it!
I created one array with all the values ($values).
After much tinkering I came up with this:

$i=0;
while($i < count($values)){
if($values[$i]!= "" && count(array_keys($values, $values[$i])) > 1){
echo "The item \"".$values[$i]."\" appears more than once. Please correct.";
echo $goback;//this prints a javascript to make the browser go back after a timeout
exit;
}
$i++;
}