Getting the keys of the common elements of 2 arrays ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pankajit09
    Contributor
    • Dec 2006
    • 296

    Getting the keys of the common elements of 2 arrays ?

    Hello,

    I have 2 arrays . One is a master array and the other slave array.

    I want the keys from the master array of the values which are present in both the arrays .
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    You can get the keys of an array using array_keys() function. So you can use this to get an array of array keys for each array. You can then use an array function in PHP to find the difference between the elements of these two arrays, that is, the elements present in one array which are not in the other. I cannot remember the name of this function offhand (am on my way out the door now) but take a look in the online PHP documentation under array functions. You will find everything you need there.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Parent and child arrays sounds more ... professional.
      ;]

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by coolsti
        You can then use an array function in PHP to find the difference between the elements of these two arrays, that is, the elements present in one array which are not in the other. I cannot remember the name of this function offhand (am on my way out the door now) but take a look in the online PHP documentation under array functions. You will find everything you need there.
        Do you mean array_diff()

        Comment

        • Tarantulus
          New Member
          • May 2007
          • 114

          #5
          array_diff() will give the elements which DON'T match, if I have read the OP correctly he wants the opposite of that (common elements) so I think array_intersect ($master,$slave ) is what he is after :)

          (as to the master/slave/parent/child nonsense, it's all political correctness gone MAD I tells ya!)

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by Tarantulus
            array_diff() will give the elements which DON'T match, if I have read the OP correctly he wants the opposite of that (common elements) so I think array_intersect ($master,$slave ) is what he is after :)

            (as to the master/slave/parent/child nonsense, it's all political correctness gone MAD I tells ya!)
            I apologise. I was just showing coolsti what he was trying to get at.

            Comment

            Working...