Jump to content

two arrays, search for values and give back...


Tanja

Recommended Posts

I have two arrays, one for sire and one for dam. In each array there are the dog id as key and the generation .

sires Array
(
    [1] => Array
        (
            [0] => 6
            [1] => 7
        )

    [3] => Array
        (
            [0] => 6
            [1] => 7
        )

    [6] => Array
        (
            [0] => 3
            [1] => 4
        )

    [18] => Array
        (
            [0] => 5
            [1] => 6
        )

    [23] => Array
        (
            [0] => 2
            [1] => 3
        )

    [43] => Array
        (
            [0] => 3
        )

    [59] => Array
        (
            [0] => 2
        )

    [73] => Array
        (
            [0] => 3
            [1] => 4
        )

    [104] => Array
        (
            [0] => 1
        )

    [124] => Array
        (
            [0] => 4
            [1] => 5
        )

    [357] => Array
        (
            [0] => 5
            [1] => 6
        )

    [359] => Array
        (
            [0] => 4
            [1] => 5
        )

)
dams Array
(
    [1] => Array
        (
            [0] => 6
            [1] => 8
            [2] => 6
            [3] => 6
        )

    [2] => Array
        (
            [0] => 8
        )

    [3] => Array
        (
            [0] => 6
            [1] => 6
            [2] => 6
        )

    [4] => Array
        (
            [0] => 5
        )

    [5] => Array
        (
            [0] => 5
        )

    [15] => Array
        (
            [0] => 7
        )

    [18] => Array
        (
            [0] => 5
            [1] => 5
            [2] => 5
        )

    [21] => Array
        (
            [0] => 4
        )

    [22] => Array
        (
            [0] => 6
        )

    [28] => Array
        (
            [0] => 6
        )

    [44] => Array
        (
            [0] => 3
        )

    [45] => Array
        (
            [0] => 4
        )

    [50] => Array
        (
            [0] => 4
        )

    [66] => Array
        (
            [0] => 7
        )

    [73] => Array
        (
            [0] => 3
        )

    [74] => Array
        (
            [0] => 3
        )

    [115] => Array
        (
            [0] => 3
        )

    [116] => Array
        (
            [0] => 2
        )

    [124] => Array
        (
            [0] => 4
        )

    [149] => Array
        (
            [0] => 4
        )

    [151] => Array
        (
            [0] => 5
        )

    [265] => Array
        (
            [0] => 1
        )

    [325] => Array
        (
            [0] => 2
        )

    [328] => Array
        (
            [0] => 5
        )

    [341] => Array
        (
            [0] => 5
        )

    [342] => Array
        (
            [0] => 5
        )

    [357] => Array
        (
            [0] => 5
        )

    [359] => Array
        (
            [0] => 4
            [1] => 4
        )

    [367] => Array
        (
            [0] => 4
        )

)

for example, dog 1 is on father-side in generation 6 and 7; on motherside three times in generation 6 and once in 8.

I want to calculate the ancestor loss for each generation, so i need to get which dogs are in generation 1, in generation 2 and so on ...
I have already how many dogs there in each generation, but i need how many different there are.
In this example there are 58 dogs but only 33 different.

Any ideas?


 

Link to comment
Share on other sites

Do you mean something like this?

<?php

$sires = [
            1 =>  [6,7],
            3 =>  [6,7],
            6 =>  [3,4],
           18 =>  [5,6]
         ];

$generations = [];

foreach ($sires as $id => $gens) {
    foreach ($gens as $gen) {
        $generations[$gen][] = $id;
    }
}

// repeat for dams, then

foreach ($generations as $gen => $dogs) {
    echo "Generation: $gen  --- " . count(array_unique($dogs)) . '<br>';
}
 
?>
Link to comment
Share on other sites

Part one works perfect, the result-array is what i want.

The second part works also correct -but not in this case. In the fourth generation f.e. here will only count the fourth gen - but i must count from 1-4 (a dog is in gen 3 AND in gen 4 -in every gen only once but in fourth generation twice).

So i think i must work with merging generation an then array unique.
 

Link to comment
Share on other sites

Hmmm....

Why are you building what looks like a database as an array?  Why not simply make a table that shows the dog, sex and its parents?  Then you can do anything you want with that info instead of finagling with an array or multiple arrays.

Link to comment
Share on other sites

I get the impression that the array is the result of processing just such a database and finding which parents are in the ancestor tree and how many generations back. With pedigree dogs, the same dogs crop up on both the sire and the dam sides of the tree, hence a degree of inbreeding.

Link to comment
Share on other sites

Barand is right ;-)

for my problem - i solved based on Barands answer - with handling the given array from first part

$gen_1_to_2 = array_merge($generationshere[1], $generationshere[2]);
	$unique_gen_2 =count(array_unique($gen_1_to_2));

and so on for following generations.
Thanks for help

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.