shishicruz Posted February 9, 2013 Share Posted February 9, 2013 Could somebody pls help me with this. I have 2 arrays arr1 and arr2 <?php $arr1[0]['name'] = "Ben"; $arr1[0]['level'] = "3"; $arr1[0]['age'] = "10"; $arr1[0]['gender'] = "M"; $arr1[1]['name'] = "Chris"; $arr1[1]['level'] = "12"; $arr1[1]['age'] = "4"; $arr1[1]['gender'] = "F"; $arr2[0]['name'] = "Jack"; $arr2[0]['grade_level'] = "3"; $arr2[0]['age'] = "10"; $arr2[0]['gender'] = "F"; $arr2[1]['name'] = "Lily"; $arr2[1]['level'] = "2"; $arr2[1]['age'] = "7"; $arr2[1]['gender'] = "F"; $arr2[2]['name'] = "Chris"; $arr2[2]['level'] = "12"; $arr2[2]['age'] = "4"; $arr2[2]['gender'] = "M"; ?> I need to compare the difference between arr1 and arr2 (and vice versa) on entries that have the same level and age values and return/highlight the columns that didn't match. ex: Ben->3->10->M in arr1 will be compared to A.1 Jack->3->10->F in arr2 because they have the same level and age values Chris->12->4->F in arr1 will be compared to A.2 Chris->12->4->M in arr2 because they have the same level and age values I should be able to know which columns didn't match for A.1 the return should be name & gender for A.2 the return should be gender Desired output: I have tried array_diff and several array functions & manipulations but i can't come up with the desired result Pls help. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/274239-how-to-compare-and-get-the-difference-between-2-multidim-arrays/ Share on other sites More sharing options...
Barand Posted February 9, 2013 Share Posted February 9, 2013 Does this get you on your way? $arr1[0]['name'] = "Ben"; $arr1[0]['level'] = "3"; $arr1[0]['age'] = "10"; $arr1[0]['gender'] = "M"; $arr1[1]['name'] = "Chris"; $arr1[1]['level'] = "12"; $arr1[1]['age'] = "4"; $arr1[1]['gender'] = "F"; $arr2[0]['name'] = "Jack"; $arr2[0]['level'] = "3"; // changed "grade_level" to "level $arr2[0]['age'] = "10"; $arr2[0]['gender'] = "F"; $arr2[1]['name'] = "Lily"; $arr2[1]['level'] = "2"; $arr2[1]['age'] = "7"; $arr2[1]['gender'] = "F"; $arr2[2]['name'] = "Chris"; $arr2[2]['level'] = "12"; $arr2[2]['age'] = "4"; $arr2[2]['gender'] = "M"; $arr3 = $arr4 = array(); foreach ($arr1 as $id => $data) { $arr3["{$data['level']}-{$data['age']}"]['Arr1'] = array ($data['name'],$data['gender']); } foreach ($arr2 as $id => $data) { $arr3["{$data['level']}-{$data['age']}"]['Arr2'] = array ($data['name'],$data['gender']); } echo '<pre>',print_r($arr3, true),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/274239-how-to-compare-and-get-the-difference-between-2-multidim-arrays/#findComment-1411268 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.