Skip to content

Instantly share code, notes, and snippets.

@LarrySul
Last active July 23, 2020 15:49
Show Gist options
  • Save LarrySul/09de1657423a494ed8c0355d9d22ab87 to your computer and use it in GitHub Desktop.
Save LarrySul/09de1657423a494ed8c0355d9d22ab87 to your computer and use it in GitHub Desktop.
write a function to find and replace element in an array
<?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));
@madeskilz
Copy link

$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;
}

@madeskilz
Copy link

$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