PHP:基于多个键对多维数组对象进行分组

PHP:基于多个键对多维数组对象进行分组,php,multidimensional-array,Php,Multidimensional Array,我试图循环通过一个数组,并根据一些键对对象进行分组。 我尝试过搜索,但我认为我没有使用正确的搜索词 数组包含家谱数据、父母姓名、姓氏等。 $BARTHIS数组中的一些对象显然彼此相关,因为它们具有相同的母亲、父亲、姓氏等,我想将它们分组到一个新的$families数组中 下面概述了数据的类型以及我希望实现的目标。 你知道我该怎么做吗?我不知道该怎么做 $births = Array ( [1] => Array ( [mother_surname] =>

我试图循环通过一个数组,并根据一些键对对象进行分组。 我尝试过搜索,但我认为我没有使用正确的搜索词

数组包含家谱数据、父母姓名、姓氏等。 $BARTHIS数组中的一些对象显然彼此相关,因为它们具有相同的母亲、父亲、姓氏等,我想将它们分组到一个新的$families数组中

下面概述了数据的类型以及我希望实现的目标。 你知道我该怎么做吗?我不知道该怎么做

$births = Array (
   [1] => Array  (
            [mother_surname] => Pig
            [mother_firstname] => Mommy
            [father_surname] => Pig
            [father_firstname] => Daddy
            [birth_date] => 2005
            [child_firstname] => Peppa
         )
   [2] => Array  (
            [mother_surname] => Pig
            [mother_firstname] => Mommy
            [father_surname] => Pig
            [father_firstname] => Daddy
            [birth_date] => 2008
            [child_firstname] => George
         )
   [3] => Array  (
            [mother_surname] => Cat
            [mother_firstname] => Mrs
            [father_surname] => Cat
            [father_firstname] => Mr
            [birth_date] => 2005
            [child_firstname] => Candy
         )
)


$families = Array (
   [1] => Array  (
              [1] => Array  (
                [mother_surname] => Pig
                [mother_firstname] => Mommy
                [father_surname] => Pig
                [father_firstname] => Daddy
                [birth_date] => 2005
                [child_firstname] => Peppa
              )
              [2] => Array  (
                [mother_surname] => Pig
                [mother_firstname] => Mommy
                [father_surname] => Pig
                [father_firstname] => Daddy
                [birth_date] => 2008
                [child_firstname] => George
              )
         )
   [2] => Array  (
            [mother_surname] => Cat
            [mother_firstname] => Mrs
            [father_surname] => Cat
            [father_firstname] => Mr
            [birth_date] => 2005
            [child_firstname] => Candy
         )
)
最接近我的是:

$shiftArray = array_shift($births);
foreach($births as $birth){
      print_r(  array_intersect($shiftArray, $birth)  );
}

但这是一个失败。

看起来您可能需要第三个数组,$family\u阵列。下面是一些可能有用的伪代码

walk through births
  filter $parents_info from birth (father_surname, father_firstname, mother)surname, mother_firstname)
  if($parents_info in $family_pattern get $key)
    $families[$key][] = $birth
  else
    $family_pattern[] = $parents_info
    $key = $family_pattern count - 1 
    $families[$key][] = $birth

泰森,你让我走上了正轨,谢谢! 这是我的解决方案,以防对其他人有利。 它还考虑了家庭住址,这在我最初的问题中没有提及

$families = array();
function searchForChildren($MSN,$MFN,$FSN,$FFN,$ADD,$array) {
    global $families;
    $stack = array();
    foreach ($array as $key => $val) {
        if ($val['Mother_Surname'] == $MSN && $val['Mother_Firstname'] == $MFN && $val['Father_Surname'] == $FSN && $val['Father_Firstname'] == $FFN && $val['Family_Address'] == $ADD) {
            $info = array(
                'Mother_Surname' => $MSN,
                'Mother_Firstname' => $MFN,
                'Father_Surname' => $FSN,
                'Father_Firstname' => $FFN,
                'Family_Address' => $ADD,
                'Birth_Year' => $val['Birth_Year'],
                'Child_Surname' => $val['Child_Surname'],
                'Child_Firstname' =>  $val['Child_Firstname']  
            );
            array_push($stack,$info );
        }
    }
   array_push($families,$stack);
}

//The below function strips out any duplicates from the parents array
function arrayUnique ( $rArray ){ 
    $rReturn = array (); 
    while ( list( $key, $val ) = each ( $rArray ) ){ 
        if ( !in_array( $val, $rReturn ) ) 
        array_push( $rReturn, $val ); 
    } 
    return $rReturn; 
} 
$uniqueParents = arrayUnique($parents);

foreach($uniqueParents as $parents){ 
    $MSN = $parents['Mother_Surname'];
    $MFN = $parents['Mother_Firstname'];
    $FSN = $parents['Father_Surname'];
    $FFN = $parents['Father_Firstname'];
    $ADD = $parents['Family_Address'];
    searchForChildren($MSN,$MFN,$FSN,$FFN,$ADD, $births);
}
print_r(json_encode($families));

什么是失败?它返回什么?那么您有
出生
数组,并且想要累积
家族
数组?在这种情况下,所有母亲/父亲的名字都必须是唯一的——否则,如果没有任何唯一的标识符(如
ID
),这将失败。是否允许将家庭密钥从数字更改为家庭姓氏?如果是这种情况,只需浏览出生记录,并将其添加到
家庭[$姓氏][]
。此外,父母也可以有不同的姓氏,如波帕马和妈妈骡。多个家庭是否只能通过父母的名字来区分同一姓氏?@Azrael_404该数据集以特定位置的特定姓氏为中心,覆盖范围约为120年。这是一个失败,因为它似乎只返回$BARTHIS数组中的公共方面,基本上是姓氏,因为这是数据集的公共方面。我需要做的是遍历$BARTHIS数组,看看哪些有共同的特定值,父母的名字和姓氏,然后将他们分组为新的$FACTURES数组中的兄弟姐妹。我认为您的方法是正确的!我会告诉你我的进展的,谢谢泰森。