简体   繁体   中英

How to merge unique this two arrays?

I'm getting two jsons converted in arrays that contains all my remote jenkin jobs and all my local jenkin jobs, formated like this:

Remote jobs ($remoteJobs):


    {
      "_class" : "hudson.model.Hudson",
      "jobs" : [
        {
          "_class" : "hudson.model.FreeStyleProject",
          "name" : "hello",
          "url" : "http://10.0.0.1:8080/job/hello/"
        },
        {
          "_class" : "hudson.model.FreeStyleProject",
          "name" : "byebye",
          "url" : "http://10.0.0.1:8080/job/byebye/"
        },
        {
          "_class" : "hudson.model.FreeStyleProject",
          "name" : "HERE",
          "url" : "http://127.0.0.1:8080/job/HERE/"
        },
        {
          "_class" : "hudson.model.FreeStyleProject",
          "name" : "hey",
          "url" : "http://10.0.0.1:8080/job/hey/"
        }
      ]
    }

Local jobs ($localJobs):


    {
      "_class" : "hudson.model.Hudson",
      "jobs" : [
        {
          "_class" : "hudson.model.FreeStyleProject",
          "name" : "hello",
          "url" : "http://127.0.0.1:8080/job/hello/"
        },
        {
          "_class" : "hudson.model.FreeStyleProject",
          "name" : "byebye",
          "url" : "http://127.0.0.1:8080/job/byebye/"
        },
        {
          "_class" : "hudson.model.FreeStyleProject",
          "name" : "hey",
          "url" : "http://127.0.0.1:8080/job/hey/"
        }
      ]
    }

I need to left only the values that aren't on Local Jobs array comparing by job name

Tryed:

arrayRecursiveDiff();
array_unique();
array_merge_recursive();

Expected output:


    {
      "_class" : "hudson.model.Hudson",
      "jobs" : [
        {
          "_class" : "hudson.model.FreeStyleProject",
          "name" : "HERE",
          "url" : "http://127.0.0.1:8080/job/HERE/"
        }
      ]
    }

The following should work. I assume the decoded json is in the variables $remote and $local respectively.

$result = $remote; // copy $remote array as a base for the result array
$result['jobs'] = array_udiff(
    $remote["jobs"],
    $local["jobs"],
    function ($a, $b) {
        return $a['name'] <=> $b['name'];
    }
);
$result['jobs'] = array_values($result['jobs']);

It only takes the difference of the "jobs" part with a custom comparison function (therefore array_udiff and not just array_diff ) comparing the 'name' keys.

I added a call to array_values to reset the jobs list key numbering (shouldn't matter if you convert it back to json anyway).

Afterwards the $result variable should contain the content you expect.

The first problems is, that you've got an object, containing an array with objects. So you have to walk through an array containing objects. Using php functons to compare the array isn't easy, because you look only for remote-jobs, and not for all jobs only remote or only local. 1. Store the arrays in variables 2. Store all local jobs 3. Compare them with the remote jobs, and store those, which aren't also local jobs.

$jsonRemote = '{
  "_class" : "hudson.model.Hudson",
  "jobs" : [
    {
      "_class" : "hudson.model.FreeStyleProject",
      "name" : "hello",
      "url" : "http://10.0.0.1:8080/job/hello/"
    },
    {
      "_class" : "hudson.model.FreeStyleProject",
      "name" : "byebye",
      "url" : "http://10.0.0.1:8080/job/byebye/"
    },
    {
      "_class" : "hudson.model.FreeStyleProject",
      "name" : "HERE",
      "url" : "http://127.0.0.1:8080/job/HERE/"
    },
    {
      "_class" : "hudson.model.FreeStyleProject",
      "name" : "hey",
      "url" : "http://10.0.0.1:8080/job/hey/"
    }
  ]
}' ;

        $jsonLocal = '{
  "_class" : "hudson.model.Hudson",
  "jobs" : [
    {
      "_class" : "hudson.model.FreeStyleProject",
      "name" : "hello",
      "url" : "http://127.0.0.1:8080/job/hello/"
    },
    {
      "_class" : "hudson.model.FreeStyleProject",
      "name" : "byebye",
      "url" : "http://127.0.0.1:8080/job/byebye/"
    },
    {
      "_class" : "hudson.model.FreeStyleProject",
      "name" : "hey",
      "url" : "http://127.0.0.1:8080/job/hey/"
    }
  ]
}' ;
        $ojctRemote = json_decode($jsonRemote) ;
        $objectLocal = json_decode($jsonLocal) ;
        var_dump($ojctRemote) ;
        $arrRemot = $ojctRemote->jobs ;
        $arrLocal = $objectLocal->jobs ;
        echo '<br/><br/>' ;
        $localJobnames =  [] ;
        $onlyRemot = []  ;
        foreach ($arrLocal as $localJob) {
            $localJobnames[] = $localJob->name ;
        }
        foreach ($arrRemot as $remoteJob) {
            if(!in_array($remoteJob->name, $localJobnames)){
                $onlyRemot[] = $remoteJob ;
            }
        }
        echo '<br/><br/>' ;
        var_dump($onlyRemot) ;
        echo '<br/><br/>' ;
        var_dump(2 <=> 2) ;
        echo '<br/><br/>' ;
        var_dump(2 <=> 3) ;
        echo '<br/><br/>' ;
        var_dump(4 <=> 3) ;

Hope this helps. You cann paste this code directly in a php-file and run it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM