我试图基于许多键遍历数组和组对象。 我尝试过搜索,但我认为我没有使用正确的搜索字词。
数组包含家谱数据,母亲和父亲姓名,姓氏等。 $ births数组中的一些对象显然彼此相关,因为它们具有相同的母亲,父亲,姓氏等,并且我想将它们分组为新的$系列数组。
下面概述了数据的类型以及我想要实现的目标。 有关如何做到这一点的任何想法?我对如何接近它感到茫然,
$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) );
}
但那是失败的。
答案 0 :(得分:0)
看起来您可能需要第三个数组$ family_pattern数组。这是一些可能有帮助的伪代码
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
答案 1 :(得分:0)
$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));