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}
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);
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'];
}
}