Assign a variable inside a forach loop

I am receiving an array, and I would like to assign the attributes of the elements in the array to individual variables. For example, I want to assign a variable named user_1_name to include userInfo.name from the first userID and user_2_name to include userInfo.name from the second user ID. Here is the code as it stands now, it uses its own method called "get_friend_list":

{assign var="friend_list" value=""}
{get_friend_list snid=$TOSNID snuid=$TOUID gameid=$GAMEID assign="friend_list"}
{if $friend_list|@count > 0}
  {foreach from=$friend_list key=userId item=userInfo name=friend_list}
    {if $smarty.foreach.friend_list.index < 4 && $smarty.foreach.friend_list.index >= 0 && !empty($userInfo.user_uid) }                             
      ***VARIABLE ASSIGNMENT HERE***
    {/if}
  {/foreach}
{else}
{/if}

      

If this is an unclear question, let me know and I will try to clear it.

thank

EDIT: BTW, if you haven't noticed, I'm using Smarty.

+3


source to share


2 answers


Have you tried:

{assign var="user_{$smarty.foreach.friend_list.index}_name" value=$userInfo.name}

      



?

+2


source


Is this what you want? assign



0


source







All Articles