2016-05-17 20 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;