I have an array of the list of employee with employees ID say $listEmployees is the array name.
I have another array data about the list of allowances and deductions of any company(This is just the list) say it as $listAllowances.
Now I have last array of data about the list of Allowances and Deductions which is assigned to employees of $listEmployees say it as $listEmpAllowances.
$listEmpAllowances has data as follows:
Array
(
[0] => Array
(
[EmployeeID] => 3
[AllowanceID] => 1
[IsAllowance] => 0
[AllowanceTitle] => PF
[AllowanceAmount] => 2000
)
[1] => Array
(
[EmployeeID] => 6
[AllowanceID] => 2
[IsAllowance] => 0
[AllowanceTitle] => Gratuity
[AllowanceAmount] => 1666
)
[2] => Array
(
[EmployeeID] => 26
[AllowanceID] => 6
[IsAllowance] => 0
[AllowanceTitle] => Allowance
[AllowanceAmount] => 1000
)
[3] => Array
(
[EmployeeID] => 3
[AllowanceID] => 4
[IsAllowance] => 0
[AllowanceTitle] => Grade
[AllowanceAmount] => 1000
)
[4] => Array
(
[EmployeeID] => 5
[AllowanceID] => 5
[IsAllowance] => 0
[AllowanceTitle] => Incentive
[AllowanceAmount] => 1000
)
[5] => Array
(
[EmployeeID] => 5
[AllowanceID] => 6
[IsAllowance] => 0
[AllowanceTitle] => Bonus
[AllowanceAmount] => 1000
)
[6] => Array
(
[EmployeeID] => 5
[AllowanceID] => 7
[IsAllowance] => 1
[AllowanceTitle] => PF Deduction
[AllowanceAmount] => 4000
)
[7] => Array
(
[EmployeeID] => 3
[AllowanceID] => 8
[IsAllowance] => 1
[AllowanceTitle] => SST Deduction
[AllowanceAmount] => 200
)
[8] => Array
(
[EmployeeID] => 51
[AllowanceID] => 1
[IsAllowance] => 0
[AllowanceTitle] => PF
[AllowanceAmount] => 1200
)
[9] => Array
(
[EmployeeID] => 51
[AllowanceID] => 3
[IsAllowance] => 0
[AllowanceTitle] => Allowance
[AllowanceAmount] => 1000
)
[10] => Array
(
[EmployeeID] => 51
[AllowanceID] => 5
[IsAllowance] => 0
[AllowanceTitle] => Incentive
[AllowanceAmount] => 1000
)
[11] => Array
(
[EmployeeID] => 51
[AllowanceID] => 7
[IsAllowance] => 1
[AllowanceTitle] => PF Deduction
[AllowanceAmount] => 2400
)
)
Now, What is need is for each employees I need to check whether or not any allowances and deductions($listAllowances) is assigned to any employee in($listEmployees). If it is assigned I need the title and amount of the assigned Allowance to the employee. If any title of $listAllowances is not assigned to any employee then I need the title and the amount needs to be 0.
I tried doing this as Follows.
foreach($listEmployees as $key => $listEmployee)
{
$EmpID = $listEmployee['EmployeeID'];
GetAllowance($listEmpPayrolls, $EmpID, $listAllowances);
}
function GetAllowance($PayrollArray, $EmployeeID, $listAllowances)
{
$ArrayForThisEmp = array();
foreach($listAllowances as $key => $list){
$AllowanceTitle = strtolower($list['Title']);
foreach($PayrollArray as $key => $pay){
if($pay['EmployeeID'] == $EmployeeID){
if (strtolower($pay['AllowanceTitle']) == $AllowanceTitle)
{
$ArrayForThisEmp[] = $pay;
}
if(strtolower($pay['AllowanceTitle']) != $AllowanceTitle)
{
$ArrayForThisEmp[] =array("EmployeeID" =>$EmployeeID, "AllowanceTitle" => $pay['AllowanceTitle'], "AllowanceAmount" => 0);
}
}
}
}
echo "<pre>"; print_r($ArrayForThisEmp);
echo "---------------------END------------------";
}
Now, What can I do to get the result as I needed.
$keytwice looks dangerous and could cause the loops to bother each other if you're noticing problems with them. In fact neither$key =>in either loop is used. You can drop that part altogether.