Coder1 advice is very good - it does not allow you to fill the namespace of global variables with a lot of unwanted messages. This is probably the most "elegant". It may not be the easiest to use if you are fairly new to PHP (which, I suppose, might happen if you find it hard to find your head around returned arrays, but that's fine).
However, if this is really a priority, you probably do not need to have one additional global variable.
I suppose I can state the obvious here, but you can do it at almost any moment of execution (if you need the necessary information, for example, the user variable $ was filled), follow these steps:
$GLOBALS['users_friends_ids'] =
Then in your template you will get access to this ...
$friendsArray = $GLOBALS['users_friends_ids'];
Or you can just use the construct
global $user_friends_ids;
when you want to initialize a variable or access it inside a function or class (which is the case for your template files - they are called inside functions, so you need to globalize or use the $GLOBALS array, which "automatically" all the variables that operate in the global namespace )
The most โlogicalโ place for this would be inside the module using one of the many hooks available to execute this code only once. hook_init() can do this for you if the user object is already loaded at this point (not sure if you will have to test it). But you may not want to compute Drupal modules (this is not so difficult).
If you do this inside a template (and although this is not a good practice, many Drupal site owners with an initial knowledge of PHP put everything into templates), you need to know what template code is executed when. Node template code is usually executed before the page template code, which is logical, because otherwise the variables for Node content in the page template will not be filled.
If you have lists of nodes, they will call this code several times, so in the end you will do something similar to what Coder1 describes. If you do not want to create your own small module, you can place a function declaration that he wrote in the theme theme.php file, since it was called only once. You do not want to place function declarations in tpl.php files, as they are sometimes called more than once (and you are not allowed to declare functions more than once).
If you find it difficult to understand the function and return, you can always do something similar in your code (which is very, very inelegant), but it is better to have an inelegant code that you understand than an elegant code that you don't).
if(!isset($GLOBALS['users_friends_ids'])) { $GLOBALS['users_friends_ids'] = }