I want to compare two variables and find the unmatched data and save it to a json in php
$month_name = array(
["months" =>"april"],
["months" =>"may"],
["months" =>"june"],
["months" =>"july"],
["months" =>"august"],
["months" =>"september"],
["months" =>"october"],
["months" =>"november"],
["months" =>"december"],
["months" =>"january"],
["months" =>"february"],
["months" =>"march"]
);
$test=json_encode($month_name);
$due_months=$this->dashmodel->get_due_month_by($fee_type_f);
$test2=json_encode($due_months);
test=[{"months":"april"},{"months":"may"},{"months":"june"},{"months":"july"},{"months":"august"},{"months":"september"},{"months":"october"},{"months":"november"},{"months":"december"},{"months":"january"},{"months":"february"},{"months":"march"}]
test2= [{"months":"april"},{"months":"may"},{"months":"october"}]
I want to get the unmatch month names of the data
You can use array_diff or array_diff_assoc. Both of them compare array1 to the given array(s), and shows which entries from array1 are missing in them.
Demonstration: https://ideone.com/W4Cp1V
$month_name = [
["months" =>"april"],
["months" =>"may"],
["months" =>"june"],
["months" =>"july"],
["months" =>"august"],
["months" =>"september"],
["months" =>"october"],
["months" =>"november"],
["months" =>"december"],
["months" =>"january"],
["months" =>"february"],
["months" =>"march"]
];
$months = [
["months" => "april"],
["months" => "may"],
["months" => "october"]
];
$due_months = array_diff_assoc($month_name, $months);
print_r($due_months);
This outputs:
Array
(
[3] => Array
(
[months] => july
)
[4] => Array
(
[months] => august
)
[5] => Array
(
[months] => september
)
[6] => Array
(
[months] => october
)
[7] => Array
(
[months] => november
)
[8] => Array
(
[months] => december
)
[9] => Array
(
[months] => january
)
[10] => Array
(
[months] => february
)
[11] => Array
(
[months] => march
)
)