2

First of all to explain what I am trying to do: I make array as a family tree of somebody. I take two persons, make their family trees from information in my mysql database, and then I want to check if they have any family connections. Like lets say personA's grandfather could be personB's great-grandfather. It is very important to know if the family connection exists in which level it exists. I mean I must to know if for example personA's grandfather is personB's great-grandfather. It would mean that the connection is between array a level 2 array and array b level 3 array. I must to know these numbers 2 and 3 in this situation.

So I have two multidimensional arrays with name a and b. I need to find out if there is any multiple values between array a and b and if there are some multiple values I must to find out where are they located in array a and array b.


My arrays looks like that:

[0]=> array(4) { 
    ["id"]=> "1" 
    ["father"]=> [0]=> array(4) { 
                     ["id"]=> "11" 
                     ["father"]=> [0]=> array(4) { 
                                      ["id"]=> "111" 
                                      ["father"]=> "" 
                                      ["mother"]=> ""
                                  }  
                     ["mother"]=> [0]=> array(4) { 
                                      ["id"]=> "112" 
                                      ["father"]=> "" 
                                      ["mother"]=> ""
                                  }  
                 } 
    ["mother"]=> [0]=> array(4) { 
                     ["id"]=> "12" 
                     ["father"]=> [0]=> array(4) { 
                                      ["id"]=> "121" 
                                      ["father"]=> "" 
                                      ["mother"]=> ""
                                  }  
                     ["mother"]=> [0]=> array(4) { 
                                      ["id"]=> "122" 
                                      ["father"]=> "" 
                                      ["mother"]=> ""
                                  }  
                 } 
}

So if I have 2 arrays like the one I showed you above, how can I check if there is any same values in arrays 'a' and 'b'?

hockeyman
  • 1,141
  • 6
  • 27
  • 57

1 Answers1

2

The algorithm here should be:

  1. Compute a set of all person IDs in each tree
  2. Intersect the IDs using array_intersect
  3. For each ID shared between trees, find it in each graph and report it in some way.

I don't know how you want to report the common ancestors, so here is the implementation for steps 1 and 2. Additionally, I provide a function for computing the "path" to a specific ID; the path is a string of letters M or F specifying where in the ancestor tree a given ID appears. For example, MF would mean the maternal grandfather (mother's father).

function gather_ids($tree) {
    if (!is_array($tree)) return array();
    return array_merge(array($tree["id"]),
                       gather_ids($tree["mother"]),
                       gather_ids($tree["father"]));
}

function common_ancestors($tree_a, $tree_b) {
    return array_intersect(gather_ids($tree_a), gather_ids($tree_b));
}

function ancestor_path_recursive($path, $tree, $id) {
    if (!is_array($tree)) return NULL;
    if ($tree["id"] == $id) return $path;
    $p = path_to_id_recursive($path .. "M", $tree["mother"], $id);
    if (!is_null($p)) return $p;
    return path_to_id_recursive($path .. "F", $tree["father"], $id);
}

function ancestor_path($tree, $id) {
    return ancestor_path_recursive("", $tree, $id);
}

Note that this code is untested, but you should get the general idea - it is very natural to process your array recursively.

Krzysztof Kosiński
  • 4,225
  • 2
  • 18
  • 23