Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have an array

        $rel = array('grandfather' => 0 ,
                    'grandmother' => 0 ,
                    'father' => 0 ,
                    'sister' => 0 ,
            );

I want it to compare to the array $family from

    $data['family'] = $facebook->api('/me/family?fields=name,relationship,education,location,cover');

using the foreach loop

     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

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..

share|improve this question
    
foreach($rel as $k => $val) in here, $k is the index; if the index of the array is numbered (instead of text: $array(0 => "value") instead of $array("txt" => "value");) the $k will represent a number. Otherwise you could always attach a $inc=0 before the foreach and then $inc++ within the foreach – MoshMage Aug 26 '14 at 9:06
up vote 0 down vote accepted

try this

if($k == $relationship)
{
   $rel[$k] += 1;
}
share|improve this answer
    
this worked.. thank you! – Lyner Kharl Aug 27 '14 at 5:40

It should work.

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]++;
                }
            }
        }
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.