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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器