2016-05-17 5 views
1

を変えます。これらは以下の通りです:は、キーの比較JSONオブジェクト2の値と条件が一致した場合、私は2つのJSONオブジェクト、男性遺伝学用と女性の遺伝学のための1つを持っている価値

男性:

$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を使用する。私はその場で実行する必要がどのような

は、1つの目的は、(例えばEclipsekeyを持っている場合は、2つのオブジェクトに対してkeysをチェックしていることは、それができることを意味する(ない"null"値を有するEE or Ee )他のオブジェクトをチェックし、他のオブジェクトのnullの値を小文字のeeのように置き換える必要があります。

は、私が(アレイを使用した場合)と交差配列を知っているが、私はそれがとにかく使用する権利のことだろうでもわかりませんか?

これは私が説明するのが非常に難しく、コードなので、ちょっとしたお菓子のためにお詫び申し上げます。私が何かを明確にする必要があれば、そう言いなさい。

+0

あなたの名前とあなたの投稿に署名しないでください。シグネチャ、挨拶、タグラインはStack Overflowの投稿には含まれません。 – meagar

+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]); 
    } 
} 
+0

はそれをこれを読んで見えますが、私はそれをしようとしたとき、私はちょうどそれを調整 –

+0

array'として 'は、タイプはstdClassのオブジェクトを使用することはできません取得するには、' 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

もう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; 
関連する問題