Compare 2 array

Hello,

[php]$array1 = array(‘Adem’ => 50, ‘Ahmet’ => 45, ‘Ercan’=>25, ‘Mehmet’ => 33, ‘Hasan’ => 40);
$array2 = array(‘Adem’ => 52, ‘Ahmet’ => 45, ‘Osman’ => 33, ‘Hasan’ => 40);[/php]
Main array $array1

How can I do it like the table above?

Thank you in advance

I don’t know the reasoning, but a foreach loop(s) and an if statement should do the trick?

I don’t know what the image was, but I will show comparisons just the same:

[php]$comp = array_diff($array1, $array2);
echo ‘

’;
print_r($comp);
echo ‘
’;[/php]

Thanks for reply.

Array1 is the main source.
The texts (names) are different and unique in each array. The texts in arrays are the names and the numbers are ages of them.
The names match with the number (age) nearby the name.
As in the table;
The first column is for $array1 and the second column is for $array2
Output be like;
If the name exists with the same age in both of arrays, it will look like second and sixth line. (Ahmet-45 | Ahmet-45)
ıf the name exists with different ages in two arrays, it will look like the first line. And ages will be highlighted red. (Adem-50 | Adem-52)
If the name that exists in array1 doesn’t exist in array2, it will look like third and fourth line. The second column will stay empty and the first column will be highlighted red. (Ercan-25 | )
If the name that doesn’t exist in array1 exists in array2, it will look like fifth line. The first column will stay empty and the second column will be highlighted red. ( | Osman-33)

I am beginner, Thanks for help in advance.

What is the point of this? Is it homework?

No, not homework, My amateur is needed for a project

Could someone please help?

My amateur is needed for a project

That doesn’t tell me anything. I mean, what exactly are you doing overall? Your image is nothing you would be doing in an application. Where does this array data come from? Details, details, details.

Not exactly how you have it, but this falls more inline with a readable chart:

[php]$new = array_merge($array1, $array2);

echo “

”;
echo "


";
foreach ($new as $k => $v) :
$color = ! isset($array1[$k]) || ! isset($array2[$k]) ? ‘red’ : ‘white’;
echo “”;
$color = isset($array1[$k], $array2[$k]) && $array1[$k] == $array2[$k] ? ‘white’ : ‘red’;
$column1 = isset($array1[$k]) ? $array1[$k] : ‘’;
$column2 = isset($array2[$k]) ? $array2[$k] : ‘’;
echo “”;
echo “”;
echo “”;
endforeach;
echo “
Name Old Age New Age
$k$column1$column2
”;[/php]

I have a feeling this is an XY Problem.

They usually are

Thank you very much, working

Sponsor our Newsletter | Privacy Policy | Terms of Service