0

Just trying to return an array in a function. Not having the best of luck...

( flag_friend_get_friends() is a function that returns a list of objects containing arrays )

So without a function, this works:

    <?php

    $users_friends = flag_friend_get_friends($user->uid);

      foreach ($users_friends as $id => $value) {     
        $users_friends_ids[] = $id;    
      }

    $test = $users_friends_ids;

    print $test[0];

    ?>

If I try and wrap it an a function, it doesn't work (nothing is printed)...:

         <?php

        function myfunc () {        

        $users_friends = flag_friend_get_friends($user->uid);

          foreach ($users_friends as $id => $value) {     
            $users_friends_ids[] = $id;    
            }

        return $users_friends;  

        }


        $test = myfunc();   

        print $test[0];

        ?>

What's the 'deliberate' mistake? :(

Updated code:

<?php
function myfunc () {        

$users_friends = flag_friend_get_friends($user->uid);
  foreach ($users_friends as $id => $value) {     
    $users_friends_ids[] = $id;    
    }

return $users_friends;  
}

$test = myfunc($user);  
print $test[0];
?>

3 Answers 3

3

You need to pass $userinto your function when you call it:

$test = myfunc($user)

UPDATE:

<?php
function myfunc ($user) {        

$users_friends = flag_friend_get_friends($user->uid);
  foreach ($users_friends as $id => $value) {     
    $users_friends_ids[] = $id;    
    }

return $users_friends;  
}

$test = myfunc($user);  
print $test[0];
?>

That's what your code needs to look like.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm afraid this doesn't seem to work. This is turning into a bit of a nightmare...
for the $user parameter, you also need to declare it to the function, as in "function myfunc ($user) {"
2

And you either need to pass in a reference for $users_friends_ids or declare it as global within the function.

The problem is that when you move stuff into a function it doesn't have access to your local variables any more.

2 Comments

$users_friends_ids[] should work even when not declared properly inside the the function. In this case, PHP is just creating $users_friends_ids as an array and adding elements to it.
it won't "work" because it won't be available outside of the function. granted, with the code above he's not accessing it outside of the function, but my point was that when you take code and move it into a function a common problem is that you don't have access to the same variable scope any more.
2

In drupal, $user is a global. So, you just need to define it at the top of your function ** if this function is always going to pull in the current user's friends.

function myfunc () {        
  // pull in the global $user var
  global $user;

  $users_friends = flag_friend_get_friends($user->uid);

  foreach ($users_friends as $id => $value) {     
    $users_friends_ids[] = $id;    
  }

  return $users_friends;  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.