2016-05-17 86 views
1

我有2个JSON对象,一个用于男性遗传学,一个用于女性遗传学。这些图所示:比较键值:JSON对象1中的值键值:JSON对象2中的值,如果条件匹配,则更改值

男:

$male='{ 
    "Bell-Albino":"Bb", 
    "Rainwater-Albino":"null", 
    "Tremper-Albino":"null", 
    "Murphys-Patternless":"null", 
    "Eclipse":"Ee", 
    "Marble-Eye":"null", 
    "Blizzard":"Zz", 
    "Mack-Snow":"Mm", 
    "Super-Snow":"null", 
    "Gem-Snow":"null", 
    "TUG-Snow":"null", 
    "Line-Bred-Snow":"null", 
    "Enigma":"null", 
    "White-and-Yellow":"null", 
    "Wildtype":"null", 
    "Giant":"null" 
}'; 

女:

$female='{ 
    "Bell-Albino":"BB", 
    "Rainwater-Albino":"null", 
    "Tremper-Albino":"null", 
    "Murphys-Patternless":"null", 
    "Eclipse":"null", 
    "Marble-Eye":"null", 
    "Blizzard":"zz", 
    "Mack-Snow":"mm", 
    "Super-Snow":"null", 
    "Gem-Snow":"null", 
    "TUG-Snow":"null", 
    "Line-Bred-Snow":"null", 
    "Enigma":"null", 
    "White-and-Yellow":"null", 
    "Wildtype":"null", 
    "Giant":"null" 
}'; 

如果我们把从Male对象Eclipsekey,我们有"Eclipse":"Ee",如果我们做同样的Female对象我们有"Eclipse":"null"

在遗传学中,我用EE表示显性,Ee表示隐性,ee表示空,但可用于基因计算。

我需要在飞行中做什么,就是检查keys为2名对象,如果一个对象具有keyEclipse例如),其具有的值是"null"(这意味着它可能是EE or Ee )需要检查另一个对象,并用另一个对象中的null值替换为小写字母,如ee

我知道数组相交(当使用数组时),但我甚至不确定这是否是正确的使用呢?

这对我来说很难解释和编码,所以对于一些华而不实的道歉。如果我需要澄清任何事情,请说出来。

+0

请不要用你的名字在你的帖子上签名。签名,称呼和标语不属于您的堆栈溢出帖子。 – meagar

+0

Alrighty会记得 –

回答

1
//convert to arrays 
$male = json_decode($male, true); 
$female = json_decode($female, true); 

foreach($male as $key => $value) { 
    if ($value != 'null' && $female[$key] == 'null') { 
     $female[$key] = strtolower($value); 
    } else { 
    if ($female[$key] != 'null' && $value == 'null') 
     $male[$key] = strtolower($female[$key]); 
    } 
} 
+0

阅读它看起来像是正确的方向,但是当我尝试它时,我得到'不能使用stdClass类型的对象作为数组' –

+0

我刚调整它,'true'选项丢失 – RST

+0

精彩,像魅力一样工作。非常感谢你 –

-1

让我知道这是否符合该法案:

for(i in $male){ 
    if ($male[i] != 'null' && $female[i] == 'null'){ 
    $female[i] = 'ee'; 
    } 
} 

for(i in $female){ 
    if ($female[i] != 'null' && $male[i] == 'null'){ 
    $male[i] = 'ee'; 
    } 
} 

你可以在这里进行测试:

https://jsfiddle.net/tz746xdz/1/

...和PHP版本:

$maleDecode = json_decode($male); 
$femaleDecode = json_decode($female); 
foreach ($maleDecode as $key=>$val){ 
    if ($maleDecode->$key != 'null' and $femaleDecode->$key == 'null') 
    $femaleDecode->$key = 'ee'; 
} 
foreach ($femaleDecode as $key=>$val){ 
    if ($femaleDecode->$key != 'null' and $maleDecode->$key == 'null') 
    $maleDecode->$key = 'ee'; 
} 
+0

这是用于JS还是PHP? –

0
$male = json_decode($male); 
$female = json_decode($female); 

foreach($male as $key => $value){ 
    if(array_key_exists($key, $female)){ 
     if($value === "null"){ 
      $male->{$key} = $female->{$key}; 
     } 
    } 
} 

foreach($female as $key => $value){ 
    if(array_key_exists($key, $male)){ 
     if($value === "null"){ 
      $female->{$key} = $male->{$key}; 
     } 
    } 
} 

print_r($male); 
print_r($female); 

// convert back to json if you need to? 
$male = json_encode($male); 
$female = json_encode($female); 
1

另一个选项。如果值为'null',则可以用其他性别的小写值覆盖它,而不检查它是什么。如果另一个值为'null',则没有任何变化,如果不是,则得到小写的任何值。

$genders = array_map('json_decode', [$male, $female]); 

foreach ($genders as $gender => $values) { 
    foreach ($values as $key => $value) { 
     // overwrite all nulls with lowercase corresponding value of other gender 
     if ($value == 'null') $genders[$gender]->$key = strtolower($genders[!$gender]->$key); 
    } 
} 

list($male, $female) = $genders;