我有2个JSON对象,一个用于男性遗传,一个用于女性遗传。这些如下所示:
男性:
$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"
}';
如果我们从Eclipse
对象中获取key
Male
,我们就会"Eclipse":"Ee"
,如果我们对Female
对象执行相同操作{ {1}}。
在遗传学中,我使用"Eclipse":"null"
表示显性,EE
表示隐性,Ee
表示空值,但可用于基因计算。
我需要动态做的是检查ee
是否有2个对象,以及一个对象是否有keys
(例如key
),其值是不 Eclipse
(这意味着它可能是"null"
)它需要检查另一个对象并用小写字母替换另一个对象中的EE or Ee
值,如{ {1}}。
我知道数组相交(当使用数组时),但我甚至不确定这是否正确使用?
这对我来说很难解释和编码,所以请稍微犹豫不决。如果我需要澄清任何事情,请说出来。
答案 0 :(得分: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]);
}
}
答案 1 :(得分: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;
答案 2 :(得分:0)
response.stream.write "hello world\n"
答案 3 :(得分:-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';
}