用条件过滤php中的数据

时间:2018-03-30 08:51:19

标签: php arrays

我有一个员工列表数组,员工ID为$listEmployees,是数组名称。

我有另一个关于任何公司的津贴和扣除列表的数组数据(这只是列表),称为$listAllowances

现在我有关于允许和扣减列表的最新数据数据,这些数据已分配给$listEmployees的员工,称为$listEmpAllowances

$listEmpAllowances的数据如下:

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
        )

)

现在,我需要检查每个员工是否需要检查($listAllowances)中是否有任何津贴和扣除($listEmployees)分配给任何员工。如果已分配,我需要为员工分配的津贴的名称和金额。如果$listAllowances的任何标题未分配给任何员工,则我需要标题,金额需要为0

我尝试按照以下方式执行此操作。

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

现在,我可以做些什么来获得我需要的结果。

3 个答案:

答案 0 :(得分:1)

试一试:

<?php

$listEmployees = array_map(function($n) { return ['EmployeeID' => "$n"]; }, range(1, 100) );
$listEmpPayrolls = array_map(function($n) { return ['EmployeeID' => "$n"]; }, range(1, 100) );

$listAllowances = [
  ['AllowanceID' => '1','IsAllowance' => '0','AllowanceTitle' => 'PF'],
  ['AllowanceID' => '2','IsAllowance' => '0','AllowanceTitle' => 'Gratuity'],
  ['AllowanceID' => '3','IsAllowance' => '0','AllowanceTitle' => 'Allowance'],
  ['AllowanceID' => '4','IsAllowance' => '0','AllowanceTitle' => 'Grade'],
  ['AllowanceID' => '5','IsAllowance' => '0','AllowanceTitle' => 'Incentive'],
  ['AllowanceID' => '6','IsAllowance' => '0','AllowanceTitle' => 'Bonus'],
  ['AllowanceID' => '7','IsAllowance' => '1','AllowanceTitle' => 'PF Deduction'],
  ['AllowanceID' => '8','IsAllowance' => '1','AllowanceTitle' => 'SST Deduction']
];

$listEmpAllowances = [
  ['EmployeeID' => '3','AllowanceID' => '1','IsAllowance' => '0','AllowanceTitle' => 'PF','AllowanceAmount' => '2000'],
  ['EmployeeID' => '6','AllowanceID' => '2','IsAllowance' => '0','AllowanceTitle' => 'Gratuity','AllowanceAmount' => '1666'],
  ['EmployeeID' => '26','AllowanceID' => '6','IsAllowance' => '0','AllowanceTitle' => 'Allowance','AllowanceAmount' => '1000'],
  ['EmployeeID' => '3','AllowanceID' => '4','IsAllowance' => '0','AllowanceTitle' => 'Grade','AllowanceAmount' => '1000'],
  ['EmployeeID' => '5','AllowanceID' => '5','IsAllowance' => '0','AllowanceTitle' => 'Incentive','AllowanceAmount' => '1000'],
  ['EmployeeID' => '5','AllowanceID' => '6','IsAllowance' => '0','AllowanceTitle' => 'Bonus','AllowanceAmount' => '1000'],
  ['EmployeeID' => '5','AllowanceID' => '7','IsAllowance' => '1','AllowanceTitle' => 'PF Deduction','AllowanceAmount' => '4000'],
  ['EmployeeID' => '3','AllowanceID' => '8','IsAllowance' => '1','AllowanceTitle' => 'SST Deduction','AllowanceAmount' => '200'],
  ['EmployeeID' => '51','AllowanceID' => '1','IsAllowance' => '0','AllowanceTitle' => 'PF','AllowanceAmount' => '1200'],
  ['EmployeeID' => '51','AllowanceID' => '3','IsAllowance' => '0','AllowanceTitle' => 'Allowance','AllowanceAmount' => '1000'],
  ['EmployeeID' => '51','AllowanceID' => '5','IsAllowance' => '0','AllowanceTitle' => 'Incentive','AllowanceAmount' => '1000'],
  ['EmployeeID' => '51','AllowanceID' => '7','IsAllowance' => '1','AllowanceTitle' => 'PF Deduction','AllowanceAmount' => '2400'],
];

function getEmployeeIds()
{
    global $listEmployees;
    return array_map(function($emp){ return $emp['EmployeeID']; },$listEmployees);
}

function searchForEmployee($id)
{
    global $listEmpAllowances;
    return array_filter($listEmpAllowances, function($emp) use ($id){return $emp['EmployeeID'] == $id;});
}

function getEmployeeAllowances($id)
{
    global $listAllowances;
    $infoArray = searchForEmployee($id);

    $out = [];

    foreach($listAllowances as $a)
    {
        $employeeContainsAllowanceTitle = false;
        foreach($infoArray as $info)
        {
            if( strtolower($a['AllowanceTitle']) == strtolower($info['AllowanceTitle']) )
            {
                $out[] = $info;
                $employeeContainsAllowanceTitle = true;
            } 
        }
        if(!$employeeContainsAllowanceTitle)
        {
            $out[] = ["EmployeeID" =>$id, "AllowanceTitle" => $a['AllowanceTitle'], "AllowanceAmount" => 0];
        }
    }

    return $out;
}

foreach(getEmployeeIds() as $id)
{
    print_r(getEmployeeAllowances($id));
}

答案 1 :(得分:0)

更改

From - $ArrayForThisEmp[] = $pay;
to - $ArrayForThisEmp[$EmployeeID][] = $pay;

From - $ArrayForThisEmp[] =array("EmployeeID" =>$EmployeeID, "AllowanceTitle" => $pay['AllowanceTitle'], "AllowanceAmount" => 0);
To - $ArrayForThisEmp[$EmployeeID][] =array("EmployeeID" =>$EmployeeID, "AllowanceTitle" => $pay['AllowanceTitle'], "AllowanceAmount" => 0);

所以,它会像下面那样llok,

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[$EmployeeID][] = $pay;
                }
                if(strtolower($pay['AllowanceTitle']) != $AllowanceTitle)
                {
                    $ArrayForThisEmp[$EmployeeID][] =array("EmployeeID" =>$EmployeeID, "AllowanceTitle" => $pay['AllowanceTitle'], "AllowanceAmount" => 0);
                }
            }
        }
    }

    echo "<pre>"; print_r($ArrayForThisEmp);
    echo "---------------------END------------------";
}

因此,您将得到一个员工明智的阵列。该数组的计数将为您提供员工的结果,无论是否为($ listEmployees)中的任何员工分配了任何津贴和扣除额($ listAllowances)。

答案 2 :(得分:0)

这是我遇到的困难。在一轮中,我获得了一个有用的值,其他未使用的如下。

Array
    (
        [3] => Array
            (
                [0] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceID] => 1
                        [IsAllowance] => 0
                        [AllowanceTitle] => PF
                        [AllowanceAmount] => 2000
                    )

                [1] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Gratuity
                        [AllowanceAmount] => 0
                    )

                [2] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Grade
                        [AllowanceAmount] => 0
                    )

                [3] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Incentive
                        [AllowanceAmount] => 0
                    )

                [4] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Bonus
                        [AllowanceAmount] => 0
                    )

                [5] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF Deduction
                        [AllowanceAmount] => 0
                    )

                [6] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => SST Deduction
                        [AllowanceAmount] => 0
                    )

                [7] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF
                        [AllowanceAmount] => 0
                    )

                [8] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceID] => 2
                        [IsAllowance] => 0
                        [AllowanceTitle] => Gratuity
                        [AllowanceAmount] => 1666
                    )

                [9] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Grade
                        [AllowanceAmount] => 0
                    )

                [10] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Incentive
                        [AllowanceAmount] => 0
                    )

                [11] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Bonus
                        [AllowanceAmount] => 0
                    )

                [12] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF Deduction
                        [AllowanceAmount] => 0
                    )

                [13] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => SST Deduction
                        [AllowanceAmount] => 0
                    )

                [14] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF
                        [AllowanceAmount] => 0
                    )

                [15] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Gratuity
                        [AllowanceAmount] => 0
                    )

                [16] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Grade
                        [AllowanceAmount] => 0
                    )

                [17] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Incentive
                        [AllowanceAmount] => 0
                    )

                [18] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Bonus
                        [AllowanceAmount] => 0
                    )

                [19] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF Deduction
                        [AllowanceAmount] => 0
                    )

                [20] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => SST Deduction
                        [AllowanceAmount] => 0
                    )

                [21] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF
                        [AllowanceAmount] => 0
                    )

                [22] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Gratuity
                        [AllowanceAmount] => 0
                    )

                [23] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceID] => 4
                        [IsAllowance] => 0
                        [AllowanceTitle] => Grade
                        [AllowanceAmount] => 1000
                    )

                [24] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Incentive
                        [AllowanceAmount] => 0
                    )

                [25] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Bonus
                        [AllowanceAmount] => 0
                    )

                [26] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF Deduction
                        [AllowanceAmount] => 0
                    )

                [27] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => SST Deduction
                        [AllowanceAmount] => 0
                    )

                [28] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF
                        [AllowanceAmount] => 0
                    )

                [29] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Gratuity
                        [AllowanceAmount] => 0
                    )

                [30] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Grade
                        [AllowanceAmount] => 0
                    )

                [31] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceID] => 5
                        [IsAllowance] => 0
                        [AllowanceTitle] => Incentive
                        [AllowanceAmount] => 1000
                    )

                [32] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Bonus
                        [AllowanceAmount] => 0
                    )

                [33] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF Deduction
                        [AllowanceAmount] => 0
                    )

                [34] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => SST Deduction
                        [AllowanceAmount] => 0
                    )

                [35] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF
                        [AllowanceAmount] => 0
                    )

                [36] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Gratuity
                        [AllowanceAmount] => 0
                    )

                [37] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Grade
                        [AllowanceAmount] => 0
                    )

                [38] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Incentive
                        [AllowanceAmount] => 0
                    )

                [39] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceID] => 6
                        [IsAllowance] => 0
                        [AllowanceTitle] => Bonus
                        [AllowanceAmount] => 1000
                    )

                [40] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF Deduction
                        [AllowanceAmount] => 0
                    )

                [41] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => SST Deduction
                        [AllowanceAmount] => 0
                    )

                [42] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF
                        [AllowanceAmount] => 0
                    )

                [43] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Gratuity
                        [AllowanceAmount] => 0
                    )

                [44] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Grade
                        [AllowanceAmount] => 0
                    )

                [45] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Incentive
                        [AllowanceAmount] => 0
                    )

                [46] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Bonus
                        [AllowanceAmount] => 0
                    )

                [47] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceID] => 7
                        [IsAllowance] => 1
                        [AllowanceTitle] => PF Deduction
                        [AllowanceAmount] => 4000
                    )

                [48] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => SST Deduction
                        [AllowanceAmount] => 0
                    )

                [49] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF
                        [AllowanceAmount] => 0
                    )

                [50] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Gratuity
                        [AllowanceAmount] => 0
                    )

                [51] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Grade
                        [AllowanceAmount] => 0
                    )

                [52] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Incentive
                        [AllowanceAmount] => 0
                    )

                [53] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => Bonus
                        [AllowanceAmount] => 0
                    )

                [54] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceTitle] => PF Deduction
                        [AllowanceAmount] => 0
                    )

                [55] => Array
                    (
                        [EmployeeID] => 3
                        [AllowanceID] => 8
                        [IsAllowance] => 1
                        [AllowanceTitle] => SST Deduction
                        [AllowanceAmount] => 200
                    )

            )

    )
    ---------------------END------------------