我有一个数组
$rel = array('grandfather' => 0 ,
'grandmother' => 0 ,
'father' => 0 ,
'sister' => 0 ,
);
我希望它与
中的数组$ family进行比较 $data['family'] = $facebook->api('/me/family?fields=name,relationship,education,location,cover');
使用foreach循环
foreach ($family as $families) {
foreach ($families as $fams) {
$id = $fams['id'];
$name = $fams['name'];
$relationship = $fams['relationship'];
foreach($rel as $k => $val){
if($k == $relationship)
{
$val += 1;
}
}
}
}
我希望$val
在每次迭代中递增,这样如果我有5个姐妹......我什么时候会
print_r($rel);
结果将是
Array ( [grandfather] => 0 [grandmother] => 0 [father] => 0 [sister] => 5 )
我已经尝试了上面的代码,但它仍然返回0 ..
答案 0 :(得分:2)
试试这个
if($k == $relationship)
{
$rel[$k] += 1;
}
答案 1 :(得分:0)
它应该有用。
foreach($ family as $ families){
foreach ($families as $fams) {
$id = $fams['id'];
$name = $fams['name'];
$relationship = $fams['relationship'];
foreach($rel as $k => $val){
if($k == $relationship)
{
$rel[$relationship]++;
}
}
}
}