galvin Posted August 24, 2011 Share Posted August 24, 2011 Say I already have the two example arrays below ($arrayA and $arrayB). What is the proper way to cycle through these arrays to ultimately create a single array ($arrayC) that only has the items that are in Array B but are NOT in Array A? Basically subtracting the first array from the second array... Array A: 1-dog 2-cat 4-fish 7-monkey 9-bird Array B: 1-dog 2-cat 3-elephant 4-fish 5-whale 6-dolphin 7-monkey 8-aardvark 9-bird 10-stingray So again, I want the end result to be this array... Array C: 3-elephant 5-whale 6-dolphin 8-aardvark 10-stingray Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/245580-subtract-two-arrays/ Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 array_diff Quote Link to comment https://forums.phpfreaks.com/topic/245580-subtract-two-arrays/#findComment-1261330 Share on other sites More sharing options...
galvin Posted August 24, 2011 Author Share Posted August 24, 2011 Awesome thanks! Quote Link to comment https://forums.phpfreaks.com/topic/245580-subtract-two-arrays/#findComment-1261372 Share on other sites More sharing options...
galvin Posted August 24, 2011 Author Share Posted August 24, 2011 Sorry, thought this was working, but something's not right. Below shows the output of my three arrays. This is what I am doing to try and get the difference... $remainingteams = array_diff($allteams, $chosenteams); $allteams... Array ( [0] => Array ( [0] => 1 [teamid] => 1 [1] => Philadelphia [teamcity] => Philadelphia [2] => Eagles [teamname] => Eagles ) [1] => Array ( [0] => 2 [teamid] => 2 [1] => Dallas [teamcity] => Dallas [2] => Cowboys [teamname] => Cowboys ) [2] => Array ( [0] => 3 [teamid] => 3 [1] => New York [teamcity] => New York [2] => Giants [teamname] => Giants ) [3] => Array ( [0] => 4 [teamid] => 4 [1] => Washington [teamcity] => Washington [2] => Redskins [teamname] => Redskins ) [4] => Array ( [0] => 5 [teamid] => 5 [1] => Detroit [teamcity] => Detroit [2] => Lions [teamname] => Lions ) [5] => Array ( [0] => 6 [teamid] => 6 [1] => Minnesota [teamcity] => Minnesota [2] => Vikings [teamname] => Vikings ) [6] => Array ( [0] => 7 [teamid] => 7 [1] => Green Bay [teamcity] => Green Bay [2] => Packers [teamname] => Packers ) [7] => Array ( [0] => 8 [teamid] => 8 [1] => Chicago [teamcity] => Chicago [2] => Bears [teamname] => Bears ) [8] => Array ( [0] => 9 [teamid] => 9 [1] => Tampa Bay [teamcity] => Tampa Bay [2] => Buccs [teamname] => Buccs ) [9] => Array ( [0] => 10 [teamid] => 10 [1] => New Orleans [teamcity] => New Orleans [2] => Saints [teamname] => Saints ) [10] => Array ( [0] => 11 [teamid] => 11 [1] => Carolina [teamcity] => Carolina [2] => Panthers [teamname] => Panthers ) [11] => Array ( [0] => 12 [teamid] => 12 [1] => Atlanta [teamcity] => Atlanta [2] => Falcons [teamname] => Falcons ) [12] => Array ( [0] => 13 [teamid] => 13 [1] => Seattle [teamcity] => Seattle [2] => Seahawks [teamname] => Seahawks ) [13] => Array ( [0] => 14 [teamid] => 14 [1] => San Francisco [teamcity] => San Francisco [2] => 49ers [teamname] => 49ers ) [14] => Array ( [0] => 15 [teamid] => 15 [1] => St. Louis [teamcity] => St. Louis [2] => Rams [teamname] => Rams ) [15] => Array ( [0] => 16 [teamid] => 16 [1] => Arizona [teamcity] => Arizona [2] => Cardinals [teamname] => Cardinals ) [16] => Array ( [0] => 17 [teamid] => 17 [1] => New York [teamcity] => New York [2] => Jets [teamname] => Jets ) [17] => Array ( [0] => 18 [teamid] => 18 [1] => Miami [teamcity] => Miami [2] => Dolphins [teamname] => Dolphins ) [18] => Array ( [0] => 19 [teamid] => 19 [1] => Buffalo [teamcity] => Buffalo [2] => Bills [teamname] => Bills ) [19] => Array ( [0] => 20 [teamid] => 20 [1] => New England [teamcity] => New England [2] => Patriots [teamname] => Patriots ) [20] => Array ( [0] => 21 [teamid] => 21 [1] => Baltimore [teamcity] => Baltimore [2] => Ravens [teamname] => Ravens ) [21] => Array ( [0] => 22 [teamid] => 22 [1] => Cincinnati [teamcity] => Cincinnati [2] => Bengals [teamname] => Bengals ) [22] => Array ( [0] => 23 [teamid] => 23 [1] => Pittsburgh [teamcity] => Pittsburgh [2] => Steelers [teamname] => Steelers ) [23] => Array ( [0] => 24 [teamid] => 24 [1] => Cleveland [teamcity] => Cleveland [2] => Browns [teamname] => Browns ) [24] => Array ( [0] => 25 [teamid] => 25 [1] => Houston [teamcity] => Houston [2] => Texans [teamname] => Texans ) [25] => Array ( [0] => 26 [teamid] => 26 [1] => Tennessee [teamcity] => Tennessee [2] => Titans [teamname] => Titans ) [26] => Array ( [0] => 27 [teamid] => 27 [1] => Jacksonville [teamcity] => Jacksonville [2] => Jaguars [teamname] => Jaguars ) [27] => Array ( [0] => 28 [teamid] => 28 [1] => Indianapolis [teamcity] => Indianapolis [2] => Colts [teamname] => Colts ) [28] => Array ( [0] => 29 [teamid] => 29 [1] => Denver [teamcity] => Denver [2] => Broncos [teamname] => Broncos ) [29] => Array ( [0] => 30 [teamid] => 30 [1] => Kansas City [teamcity] => Kansas City [2] => Chiefs [teamname] => Chiefs ) [30] => Array ( [0] => 31 [teamid] => 31 [1] => Oakland [teamcity] => Oakland [2] => Raiders [teamname] => Raiders ) [31] => Array ( [0] => 32 [teamid] => 32 [1] => San Diego [teamcity] => San Diego [2] => Chargers [teamname] => Chargers ) ) $chosenteams... Array ( [0] => Array ( [0] => 1 [teamid] => 1 [1] => Philadelphia [teamcity] => Philadelphia [2] => Eagles [teamname] => Eagles ) ) $remainingteams... (this should be all teams EXCEPT the Eagles, but it's empty for some reason) Array ( ) Any ideas what I'm doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/245580-subtract-two-arrays/#findComment-1261399 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 well, that's hardly the example you gave in your first post... You had simple arrays, and now you have mulch-dimensional arrays, there's a big difference. Quote Link to comment https://forums.phpfreaks.com/topic/245580-subtract-two-arrays/#findComment-1261402 Share on other sites More sharing options...
galvin Posted August 24, 2011 Author Share Posted August 24, 2011 Sorry, I'm extremely ignorant/inept when it comes to arrays Can I still do it using array_diff(), or is that function useless with multidimensional arrays? Quote Link to comment https://forums.phpfreaks.com/topic/245580-subtract-two-arrays/#findComment-1261405 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 as the manual says: This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);. Quote Link to comment https://forums.phpfreaks.com/topic/245580-subtract-two-arrays/#findComment-1261411 Share on other sites More sharing options...
galvin Posted August 24, 2011 Author Share Posted August 24, 2011 ok thank you again Quote Link to comment https://forums.phpfreaks.com/topic/245580-subtract-two-arrays/#findComment-1261414 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.