Hi

A user on my site chooses 2 options per row of data from 2 spearate list boxes.

e.g. first list box contains type of foods (apples, oranges, bananas) and the second list box contains a quantity, lets say 1-10.

So I choose Apples from list box one and the number 3 from list box two.

I am storing these choices into 2 session arrays.

To ensure a user has chosen an option from each list box a row, I use the following code:

$comparison = array_diff_assoc($_SESSION['S_FOOD_SELECTION'], $_SESSION['S_FOOD_QUANTITIES']);

This gives me any differences beteeen the session arrays but the problem is, it doesn't work if one of the arrays is empty!

Is there a similar function that will calculate if there is a difference between two arrays, even if one if them is completely empty?

Many Thanks
kbc

    Why not count the number of elements in each array? If the count of both arrays are greater than 1 they chose an option.

      Maybe I'm misunderstanding what you're doing, but from what I can see if one of the arrays contains fruits and the other contains numbers then the difference is going to be quite large, because fruits aren't numbers (although I did have a pear today that was remarkably 9-shaped). Nor do I see why you want to separate the two pieces of information from each other into two separate arrays, instead of keeping fruits and quantities paired together.

      But none of the array_diff functions would tell you accurately if there is a change, they'll only show elements in the first array that aren't in the second. They won't, for example, return any elements in the second array.

        Write a Reply...