1

I have 2 JSON objects, one for Male Genetics and one for Female Genetics. These are shown below:

Male:

$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:

$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" 
}';

If we take the Eclipse key from the Male object, we have "Eclipse":"Ee" and if we do the same for the Female object we have "Eclipse":"null".

In the genetics I use EE to signify dominant, Ee to signify recessive and ee to signify null but can be used in gene calculation.

What I need to do on the fly, is check the keys for the 2 objects and if one object has a key (Eclipse for example) that has a value that is not "null" (which means it could be EE or Ee) it needs to check the other object and replace the null value in the other object with lowercase letters like ee.

I know of array intersect (when using arrays) but I'm not even sure that would be the right thing to use anyway?

This is quite difficult for me to explain, and code, so apologies for waffling a bit. If I need to clarify anything please say so.

2
  • Please don't sign your posts with your name. Signatures, salutations and taglines do not belong in your Stack Overflow posts. Commented May 17, 2016 at 22:12
  • Alrighty will remember that Commented May 17, 2016 at 22:12

4 Answers 4

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]);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Reading it this looks like it's the right direction, but when I try it, i get Cannot use object of type stdClass as array
I just adjusted it, the true option was missing
1

One other option. If a value is 'null', you can just overwrite it with the lowercase value of the other gender without checking what it is. If the other value is 'null' as well, nothing changes, and if it isn't you get the lowercase version of whatever it is.

$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;

Comments

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);

Comments

-1

Let me know if this fits the bill:

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';
  }
}

You can test it here:

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

... and a PHP version:

$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';
}

1 Comment

Is this for JS or PHP?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.