1

Find common values in multiple arrays with PHP

Pretty much does what I need, but in PHP, I need VB.

My situation is I am trying to create an intelligent stock pick system based on multiple stock locations.

At the point of invoicing, we loop through the items invoiced and check the stock database for available stock.

If all items on the invoice are available to pick from stock location 1, then all should be picked from stock location 1... etc.

If all most items are available from stock location 1 except one or two, then pick all stock from stock location 1 except for the exceptions which should be picked from the location with the highest available stock.

Finding highest available stock is simple, but I can't work out how to analyse the stock availability from multiple locations and finding the common stock locations.

I can create a set of arrays like this

Item ID    |     Available Stock Locations
    1      |        2, 3, 5
    2      |        1, 2, 6
    3      |        2, 3, 4
    4      |        1, 2 ,3 

How to I compare those location lists to find that 2 is common to all four?

Secondly, if one Item did not have a common stock location, how would I identify that item so I can go back and find the highest available stock level for it?

Community
  • 1
  • 1
Jamie Hartnoll
  • 7,231
  • 13
  • 58
  • 97

2 Answers2

2

As in PHP, you can intersect arrays to find common values. Thanks to LINQ, this is fairly easy in VB:

Dim array1 = {2, 3, 5}
Dim array2 = {1, 2, 6}
Dim array3 = {2, 3, 4}
Dim array4 = {1, 2, 3}

Dim commonItems = array1.Intersect(array2).Intersect(array3).Intersect(array4)

commonItems is now an IEnumerable(Of Integer) containing all common store locations.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

Linq would be cleaner, but this approach should work for 2.0...

Dim arrX(2) As Integer
arrX(0) = 0
arrX(1) = 1
arrX(2) = 2

Dim arrY(2) As Integer
arrY(0) = 0
arrY(1) = 32
arrY(2) = 2

Dim arrZ(2) As Integer
arrZ(0) = 10
arrZ(1) = 2
arrZ(2) = 22

Dim arrCommon() As Integer

For Each i As Integer In arrX

    For Each i2 As Integer In arrY

        If i = i2 Then

            For Each i3 As Integer In arrZ

                If i2 = i3 Then

                    If arrCommon Is Nothing OrElse arrCommon.Length = 0 Then

                        ReDim arrCommon(0)
                        arrCommon(0) = i

                    Else

                        ReDim Preserve arrCommon(arrCommon.Length)

                        arrCommon(arrCommon.Length - 1) = i

                    End If

                End If

            Next

        End If

    Next

Next
NoAlias
  • 9,218
  • 2
  • 27
  • 46
  • The algorithm seems ok, but why not use a `List(Of Integer)`? If a array is required you can create one by calling `arrCommon = listCommon.ToArray()` when finished. – Olivier Jacot-Descombes Dec 23 '11 at 18:13
  • I prefer generic lists too, but decided to implement this example using just arrays, since that is what was mentioned in the question. Plus, I wasn't positive off the top of my head that generics were included in 2.0. You do raise a valid question though, because the ReDim statements and one of the If Then blocks could be avoided using the List(of Integer) type. – NoAlias Dec 23 '11 at 18:26
  • I think I'm banned from computer work til after Xmas now, but this looks good, except, for one flaw, it seems I'd need to know how many stock locations I have before hard writing the code. I'm trying to make this system scalable too, so need it to be flexible if locations are added/removed. – Jamie Hartnoll Dec 23 '11 at 22:22