0

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

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Mr. jack
  • 1
  • 5
  • We've a help here on Stack Overflow related to styling the own posts: https://stackoverflow.com/editing-help ;-) – David Apr 06 '19 at 10:50
  • What is your exact desired result? Do you want to maintain a 2 dimensional structure? or just a 1 one-dimensional result? – mickmackusa Apr 06 '19 at 16:01

1 Answers1

0

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
        )

)
Yousof K.
  • 1,374
  • 13
  • 23
  • didnt get you,can u explain – Mr. jack Apr 06 '19 at 10:55
  • A PHP Error was encountered=>Array to string conversion – Mr. jack Apr 06 '19 at 11:17
  • Could you please upload your code somewhere like github or ideone so that I could take a look? Your code probably has a variable which is an array, but is being used as if it's a string. – Yousof K. Apr 06 '19 at 11:19
  • and i want to get it in json format,because i will use it to jquery – Mr. jack Apr 06 '19 at 11:19
  • You can always json_encode the `$due_months` and return it. But first let me take a look at your code. – Yousof K. Apr 06 '19 at 11:20
  • If `$paid_months` is coming from the model as an array, there is no need to encode and decode it, just use as is. If it's coming as an object, use the model's correspondent method to turn it into an array, or use a loop to create an array of months *that matches the structure of the `$month_names`*. – Yousof K. Apr 06 '19 at 11:34
  • Best do it with JavaScript: https://stackoverflow.com/a/33034768/2745485 – Yousof K. Apr 06 '19 at 11:52