我要在包含所有远程jenkin作业和所有本地jenkin作业的数组中转换两个json,格式如下:
远程作业($ 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/"
}
]
}
本地工作($ 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/"
}
]
}
我只需要按职位名称比较就不会留下“本地职位”数组中没有的值
尝试过:
arrayRecursiveDiff();
array_unique();
array_merge_recursive();
预期输出:
{
"_class" : "hudson.model.Hudson",
"jobs" : [
{
"_class" : "hudson.model.FreeStyleProject",
"name" : "HERE",
"url" : "http://127.0.0.1:8080/job/HERE/"
}
]
}
答案 0 :(得分:1)
以下应该起作用。我假设解码的json分别位于变量$remote
和$local
中。
$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']);
它仅使用“自定义”比较功能(因此array_udiff
,而不仅仅是array_diff
)来比较“名称”键,从而消除了“职位”部分的差异。
我向array_values
添加了一个电话,以重置作业列表密钥编号(无论如何,无论如何将其转换回json都没关系)。
然后,$result
变量应包含您期望的内容。
答案 1 :(得分:1)
第一个问题是,您有一个对象,其中包含带有对象的数组。因此,您必须遍历包含对象的数组。 使用php函数比较数组并不容易,因为您只查找远程作业,而不是仅查找远程或本地作业。 1.将数组存储在变量中 2.存储所有本地作业 3.将它们与远程作业进行比较,并存储它们,这些作业也不是本地作业。
$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) ;
希望这会有所帮助。您无法将此代码直接粘贴到php文件中并运行。