Last active
July 23, 2020 15:49
-
-
Save LarrySul/09de1657423a494ed8c0355d9d22ab87 to your computer and use it in GitHub Desktop.
write a function to find and replace element in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$banks = | |
[ | |
'United Bank For Africa' => 'UNITED BANK FOR AFRICA PLC', | |
'Access Bank Plc' => 'ACCESS BANK PLC', | |
'Gtb' => 'GUARANTY TRUST BANK PLC', | |
'Firstbank Nigeria plc' => 'FIRST BANK OF NIGERIA PLC' | |
]; | |
$employees = | |
[[ | |
'ID' => 1, | |
'State' => 'Lagos', | |
'Bank' => 'Access', | |
'Type' => 'full time' | |
], | |
[ | |
'ID' => 2, | |
'State' => 'Lagos', | |
'Bank' => 'Gtb', | |
'Type' => 'full time' | |
]]; | |
//Can you Match the bank name in the employee's array with the right Bank Name and return as JSON | |
function full_bank_name($arr, $banks) | |
{ | |
$ret = array(); | |
foreach($arr as $ar){ | |
$ar['Bank'] = $banks[$ar['Bank']]; | |
array_push($ret, $ar); | |
} | |
return $ret; | |
} | |
print_r(full_bank_name($employees, $banks)); |
$banks =array(
'UBA' => 'UNITED BANK FOR AFRICA PLC',
'Access' => 'ACCESS BANK PLC',
'Gtb' => 'GUARANTY TRUST BANK PLC',
'Firstbank' => 'FIRST BANK OF NIGERIA PLC'
);
$employees =
[array(
'ID' => 1,
'State' => 'Lagos',
'Bank' => 'Access',
'Type' => 'full time'
),
array(
'ID' => 2,
'State' => 'Lagos',
'Bank' => 'Gtb',
'Type' => 'full time'
)
];
//Can you Match the bank name in the employee's array with the right Bank Name and return as JSON
function full_bank_name($arr)
{
$ret = array();
foreach($arr as $ar){
$ar->Bank = $banks[$ar->Bank];
array_push($ret, $ar);
}
return $ret;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$banks =array(
'UBA' => 'UNITED BANK FOR AFRICA PLC',
'Access' => 'ACCESS BANK PLC',
'Gtb' => 'GUARANTY TRUST BANK PLC',
'Firstbank' => 'FIRST BANK OF NIGERIA PLC'
);
$employees =
[array(
'ID' => 1,
'State' => 'Lagos',
'Bank' => 'Access',
'Type' => 'full time'
),
array(
'ID' => 2,
'State' => 'Lagos',
'Bank' => 'Gtb',
'Type' => 'full time'
)
];
//Can you Match the bank name in the employee's array with the right Bank Name and return as JSON
function full_bank_name($arr)
{
$ret = array();
foreach($arr as $ar){
$ar->Bank = $banks[$ar->Bank];
array_push($ret, $ar);
}
return $ret;
}