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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douxing1969 2016-12-30 22:06
    关注

    You can get the key in foreach, and then check if it's one of the specific keys.

    $specific_keys = array(...);
    foreach ($achList as $key => $acct) {
        if (in_array($key, $specific_keys)) {
            $actNum[] = chopActNum($acct['actNum']);
        } else {
            $actNum[] = $acct['actNum'];
        }
    }
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 用C语言解决编程问题
  • ¥15 unity硬件连接与使用
  • ¥15 鱼缸加热棒的数据分析或者实际案例也行
  • ¥15 postgresql11安装完成后,pgadmin无法启动
  • ¥15 (标签-无人机|关键词-Matlab代码)
  • ¥15 执行shell脚本提示参数太多
  • ¥15 困扰好久了,电脑的串口调试助手向单片机发送一个字符(如:字符‘1’到‘8’),单片机点亮对应的灯(如发送字符‘2’则点亮第2个灯)并给电脑回复信息:已点亮第x个灯(其中 x 要替换成对应的数字)
  • ¥15 求减少这个c++程序内存的解决方案
  • ¥30 改正代码,错误比较多
  • ¥15 vue3 keepalive