Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Array diff key + value php

Can you please tell me how to compare only the date in this array?

<?php
$arr_1 = array();
$arr_2 = array();

array_push($arr_1, array("id" => "1", "date" => "2022-02-03"));
array_push($arr_2, array("id" => "1", "date" => "2022-02-04"));

$result = array_diff($arr_1, $arr_2);

echo json_encode($result);
?>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Convert the arrays to associative arrays, then compare those.

Then you can convert the result back to a 2-dimensional array.

$assoc_1 = array_combine(array_column($arr_1, 'id'), array_column($arr_1, 'date'));
$assoc_2 = array_combine(array_column($arr_2, 'id'), array_column($arr_2, 'date'));
$diff_assoc = array_diff($assoc_1, $assoc_2);
$results = [];
foreach ($diff_assoc as $id => $date) {
    results[] = ['id' => $id, 'date' => $date];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading