dongzang7182 2016-12-30 21:44
浏览 39
已采纳

如何为每个循环数组和调用函数?

I have an array that I need to loop through, and for specific keys, I then need to call chopActNum function. The function takes an account number, and turns it into the "xxxxxxxxxx1234" format. I would like to then display this new format rather than the whole account number. My $accountList holds all the information for the accounts. My confusion is in that I currently display all my account information through assignment in Smarty templating. I am horrible at explaining, so perhaps seeing my code will help elucidate my issue.
My function:

public function chopActNum($actNum=0){

    $numPlace=strlen($actNum)-4;
    $repeatX=str_repeat('x',$numPlace);
    $actNumConcat=$repeatX.substr($actNum, -4); 
    return $actNumConcat;

Php:

foreach($achList as $a)
{
$actNum[] = $a['actNum'];
$chopNum=$Ach->chopActNum($actNum);
}

$achList=$Ach->listAch($logged_userid);
$smarty->assign("accountList",$achList);

Tpl Code:

{foreach from=$accountList item=v name=foo} 
<td class="tableData">{$v.actName}</td>
<td class="tableData">{$v.actNum}</td>
<td class="tableData">{$v.bankRoute}</td>
{/foreach}
  • 写回答

2条回答 默认 最新

  • duanmengmiezen8855 2016-12-30 21:53
    关注

    use array_map to prepare the data for your template like this:

    $achList = arrar_map(function($v){
      return array(
        'actName' => $v['actName'],
        'actNum' => chopActNum($v['actNum']),
        'bankRoute' => $v['bankRoute']
      );
    }, $achList);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?