Finding unique data in 2 arrays

Discussion in 'PHP' started by redslazers, Dec 27, 2011.

  1. #1
    Hey fellow php'ers i have a slight problem:

    I have 2 arrays that look sort of like this:

    Array =>
    [0] Array( [id]=434b5g6 [unique_id]=banana [level]=8 [quality]=2)
    [1] Array( [id]=bfrfnr [unique_id]=apple [level]=4 [quality]=1)
    [2] Array( [id]=yt347509 [unique_id]=grapefruit [level]=8 [quality]=2 )
    [3] Array( [id]=456645 [unique_id]=strawberry [level]=1 [quality]=6)


    Array =>
    [0] Array( [id]=434b5g6 [unique_id]=strawberry [level]=8 [quality]=2)
    [1] Array( [id]=bfrfnr [unique_id]=apple [level]=4 [quality]=1)
    [2] Array( [id]=yt347509 [unique_id]=grapefruit [level]=8 [quality]=2 )
    [3] Array( [id]=456645 [unique_id]=pinapple [level]=1 [quality]=6)

    I need a way to remove the elements in which the [unique_id] exists in both arrays so that im left with 2 arrays which only contain elements that are unique. Eg if we were to run the script on these 2 arrays i would be left with:

    Array =>
    [0] Array( [id]=434b5g6 [unique_id]=banana [level]=8 [quality]=2)

    Array =>
    [0] Array( [id]=456645 [unique_id]=pinapple [level]=1 [quality]=6)

    Im really not sure how to do this though, i know there is array_diff but this only works for single level arrays. Im using a multi level one and only trageting the [unique_id] part of it. Any help would be really appreciated.
     
    redslazers, Dec 27, 2011 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Why not create a seperate array, loop all the array's you can and check if the unique id is not in the seperate array, if not, add it, if it exists then remove it! :)

    And even with array_diff you can do it, but its just a little more work! :) maby its a great idea to swap the id and unique id's? then you always have a `unique` unique_id
     
    EricBruggema, Dec 29, 2011 IP