Jump to content

How to compare and get the difference between 2 multidim arrays


shishicruz

Recommended Posts

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:

img.png

I have tried array_diff and several array functions & manipulations but i can't come up with the desired result :( Pls help. Thanks!

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.