[英]in a key value array, how to increment array value in foreach loop
I have an array 我有一个数组
$rel = array('grandfather' => 0 ,
'grandmother' => 0 ,
'father' => 0 ,
'sister' => 0 ,
);
I want it to compare to the array $family from 我希望它与数组$ family比较
$data['family'] = $facebook->api('/me/family?fields=name,relationship,education,location,cover');
using the foreach loop 使用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;
}
}
}
}
i want the $val
to increment in each iteration such that if I have 5 sisters.. when i will 我希望$val
在每次迭代中增加,以便如果我有5个姐妹..什么时候我会
print_r($rel);
the result would be 结果将是
Array ( [grandfather] => 0 [grandmother] => 0 [father] => 0 [sister] => 5 )
i have tried the above code but it still returns to 0.. 我已经尝试了上面的代码,但它仍然返回到0。
try this 尝试这个
if($k == $relationship)
{
$rel[$k] += 1;
}
It should work. 它应该工作。
foreach ($family as $families) { 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]++;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.