Skip to content
Advertisement

PHP – Finding the difference between Two Multidimensional Arrays with different structures based on one value

I am struggling with understanding what array function would work best for my particular situation. There seems to be a vast amount of confusion from others as well as I have read several posts.

I have two arrays with different structures but both contain an ID field I would like to compare.

Array one is my footage array… it looks something like:

array(1) {
[0]=> array(5) { 
    ["ID"]=> string(3) "172" 
    ["Name"]=> string(26) "172_535A-New_Product-1.mp4" 
    ["Projects_ID"]=> string(2) "25"        
    ["FileName_Display"]=> string(18) "535A-New_Product-1" 
    ["FileName_Real"]=> string(22) "172_535A-New_Product-1"}}

In this array ID is what I want to compare. The other array has a lot more information and is thus structured completely different BUT it also has this same ID listed as Footage_ID. It looks something like:

array(1) { 
    [0]=> array(16) { 
         ["ID"]=> string(3) "168" 
         ["Unique_Array_ID"]=> string(1) "0" 
         ["HTML_ID"]=> string(15) "Item_0_0_34_A_0"         
         ["HTML_Selector"]=> string(10) "0_0_34_A_0"        
         ["Frame_Selector"]=> string(8) "0_0_34_A"      
         ["FootageArrayKey"]=> string(1) "0" 
         ["Timeline"]=> string(1) "0" 
         ["Tab"]=> string(1) "0" 
         ["inPoint"]=> string(12) "5.7919480000"        
         ["outPoint"]=> string(13) "43.2125420000"      
         ["Duration"]=> string(13) "45.1200000000"      
         ["Section"]=> string(1) "1" 
         ["Green_Screen"]=> string(1) "1"       
         ["Place_in_Timeline"]=> string(1) "0"      
         ["Footage_ID"]=> string(3) "172" 
         ["Projects_ID"]=> string(2) "25" 
}}

With these two the Footage_ID in array #2 (172) matches the ID in array #1 (172). Therefore I do not need the entry from array #1. I am looking for only IDs not listed in array #2. This would be my return array.

First, since they are multidimensional I am a bit lost on comparing them. Are there any built in functions for this like array_diff? As far as I know array_diff is only for one dimensional array comparisons. The fact that the arrays are different structures may mean little but to me it also seems like it could be an issue for certain functions.

The function I seem to need would accept three values($array1, $array2, $key_to_compare_by). Then it would return the entries(arrays) from array1 where the $key_to_compare is not found. I have not seen this situation in particular talked about yet and was wondering if anyone has advice on how to accomplish this.

I see people talking about filtering, mapping, and other functions but there are so many different approaches and none seem to be exactly what I am facing currently. I giant part of this misunderstanding is that I DO NEED TO learn more about filtering, etc. Any advice on this problem would be helpful. Thanks!

Advertisement

Answer

If I understand the question correctly you want to return entries from the first array if the ID doesn’t exist as a Footage_ID in the second array.

If that’s the case you can use array_filter() to check if each ID from the first array exists in the array of Footage_ID‘s from the second, and if it doesn’t, return the current array:

$o = array_filter($array1, function($a1) use ($array2) {
  return ! in_array($a1['ID'], array_column($array2, 'Footage_ID'));
});

Here’s a simplified demo

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement