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'?

3
  • I think youre looking for this: stackoverflow.com/questions/5653241/… Commented Jul 10, 2015 at 7:21
  • As far as I can tell array_intersect will just remove everything, unless the checked persons are siblings and their trees are identical. Commented Jul 10, 2015 at 7:27
  • Unless I am misunderstanding this question, this page is missing a minimal reproducible example. Commented Mar 17, 2023 at 21:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.