我想基于索引合并两个关联数组

时间:2019-04-20 12:28:54

标签: php

我有两个索引为1到10和t的数组,当我必须根据索引进行合并时,所以只需要添加一些员工Salary。我已经使用array_merge()完成了,但是输出没有合并我无法获得正确的输出。输出仅是employeeSalary Array。为什么?我已经尝试过了,但是无法正常工作。

    $employee = array
(
0=>
    array("employee_id"=>1, "firstName"=>"Zahir", "lastName"=>"Alam", "Age"=>25, "Company"=>"Switchme", "Role"=>"Developer", "Department"=>"Tech"
        ,"Head"=>
            array("Id"=>3 , "Name"=>"Sourasis Roy")
    )
,
1=>
    array("employee_id"=>2, "firstName"=>"Amith", "lastName"=>"Manniken", "Age"=>25, "Company"=>"Switchme", "Role"=>"Developer", "Department"=>"Tech"
        ,"Head"=>
            array("Id"=>3 , "Name"=>"Sourasis Roy")
    )
,
2=>
    array("employee_id"=>3, "firstName"=>"Sourasis", "lastName"=>"Roy", "Age"=>28, "Company"=>"Switchme", "Role"=>"CTO")
,
3=>
    array("employee_id"=>4, "firstName"=>"Aditya", "lastName"=>"Mishra", "Age"=>29, "Company"=>"Switchme", "Department"=>"Tech", "Role"=>"CEO")
,
4=>
    array("employee_id"=>5, "firstName"=>"Priti", "lastName"=>"Lata", "Age"=>24, "Company"=>"Switchme", "Role"=>"HR")
,
5=>
    array("employee_id"=>6, "firstName"=>"Sumita", "lastName"=>"Nath", "Age"=>24, "Company"=>"Switchme", "Role"=>"HLA Head", "Department"=>"Crm")
,
6=>
    array("employee_id"=>7, "firstName"=>"Tarini", "lastName"=>"Khanna", "Age"=>22, "Company"=>"Switchme", "Role"=>"Content Writer")
,
7=>
    array("employee_id"=>8, "firstName"=>"Abhisek", "lastName"=>"Soni", "Age"=>23, "Company"=>"Switchme", "Role"=>"HLA", "Department"=>"Crm","Head"=>array("Id"=>5 , "Name"=>"Sumita Nath")
    )
,
8=>
    array("employee_id"=>9, "firstName"=>"Ankit", "lastName"=>"Pump", "Age"=>23, "Company"=>"Switchme", "Role"=>"HLA", "Department"=>"Crm"
        ,"Head"=>
            array("Id"=>5 , "Name"=>"Sumita Nath")
    )
,
9=>
    array("employee_id"=>10, "firstName"=>"Pogo", "lastName"=>"Laal", "Age"=>23, "Company"=>"Switchme", "Role"=>"Designer")
,
10=>
    array("employee_id"=>11, "firstName"=>"Sabina", "lastName"=>"Sekh", "Age"=>28, "Company"=>"Switchme", "Role"=>"HLA Head", "Department"=>"Crm")
,
11=>
    array("employee_id"=>12, "firstName"=>"Sanjay", "lastName"=>"Poudal", "Age"=>24, "Company"=>"Switchme", "Role"=>"HLA Head", "Department"=>"Crm"
        ,"Head"=>
            array("Id"=>10 , "Name"=>"Sabina Sekh")
    )
,
);
    $employee_salary = array
(
7=>
    array("employee_id"=>7, "salary"=>"55,000"
    )
,
2=>
    array("employee_id"=>2, "salary"=>"60,000"
    )
,
9=>
    array("employee_id"=>9, "salary"=>"50,000"
    )
,
10=>
    array("employee_id"=>10, "salary"=>"30,000"
    )
,
);
    $ar= array();
for($j=0;$j<count($employee);$j++)
{
    error_reporting(0);
    if($employee[$j]=$employee_salary[$j])
    {
        $ar=$employee[$j]+$employee_salary[$j];
        print_r($ar);
        echo "<br>";
    }
    error_reporting(0);
}

4 个答案:

答案 0 :(得分:2)

正如克里斯·怀特(chris white)所说,您忘记再添加一个=作为比较。并使用 isset()检查employee_salary的第二个数组中是否设置了数组索引。例如 if($ employee [$ j] == isset($ employee_salary [$ j]))

$employee = array
(
0=>
    array("employee_id"=>1, "firstName"=>"Zahir", "lastName"=>"Alam", "Age"=>25, "Company"=>"Switchme", "Role"=>"Developer", "Department"=>"Tech"
        ,"Head"=>
            array("Id"=>3 , "Name"=>"Sourasis Roy")
    )
,
1=>
    array("employee_id"=>2, "firstName"=>"Amith", "lastName"=>"Manniken", "Age"=>25, "Company"=>"Switchme", "Role"=>"Developer", "Department"=>"Tech"
        ,"Head"=>
            array("Id"=>3 , "Name"=>"Sourasis Roy")
    )
,
2=>
    array("employee_id"=>3, "firstName"=>"Sourasis", "lastName"=>"Roy", "Age"=>28, "Company"=>"Switchme", "Role"=>"CTO")
,
3=>
    array("employee_id"=>4, "firstName"=>"Aditya", "lastName"=>"Mishra", "Age"=>29, "Company"=>"Switchme", "Department"=>"Tech", "Role"=>"CEO")
,
4=>
    array("employee_id"=>5, "firstName"=>"Priti", "lastName"=>"Lata", "Age"=>24, "Company"=>"Switchme", "Role"=>"HR")
,
5=>
    array("employee_id"=>6, "firstName"=>"Sumita", "lastName"=>"Nath", "Age"=>24, "Company"=>"Switchme", "Role"=>"HLA Head", "Department"=>"Crm")
,
6=>
    array("employee_id"=>7, "firstName"=>"Tarini", "lastName"=>"Khanna", "Age"=>22, "Company"=>"Switchme", "Role"=>"Content Writer")
,
7=>
    array("employee_id"=>8, "firstName"=>"Abhisek", "lastName"=>"Soni", "Age"=>23, "Company"=>"Switchme", "Role"=>"HLA", "Department"=>"Crm","Head"=>array("Id"=>5 , "Name"=>"Sumita Nath")
    )
,
8=>
    array("employee_id"=>9, "firstName"=>"Ankit", "lastName"=>"Pump", "Age"=>23, "Company"=>"Switchme", "Role"=>"HLA", "Department"=>"Crm"
        ,"Head"=>
            array("Id"=>5 , "Name"=>"Sumita Nath")
    )
,
9=>
    array("employee_id"=>10, "firstName"=>"Pogo", "lastName"=>"Laal", "Age"=>23, "Company"=>"Switchme", "Role"=>"Designer")
,
10=>
    array("employee_id"=>11, "firstName"=>"Sabina", "lastName"=>"Sekh", "Age"=>28, "Company"=>"Switchme", "Role"=>"HLA Head", "Department"=>"Crm")
,
11=>
    array("employee_id"=>12, "firstName"=>"Sanjay", "lastName"=>"Poudal", "Age"=>24, "Company"=>"Switchme", "Role"=>"HLA Head", "Department"=>"Crm"
        ,"Head"=>
            array("Id"=>10 , "Name"=>"Sabina Sekh")
    )
,
);

$employee_salary = array
(
7=>
    array("employee_id"=>7, "salary"=>"55,000"
    )
,
2=>
    array("employee_id"=>2, "salary"=>"60,000"
    )
,
9=>
    array("employee_id"=>9, "salary"=>"50,000"
    )
,
10=>
    array("employee_id"=>10, "salary"=>"30,000"
    )
,
);

$ar= array();
for($j=0;$j<count($employee);$j++)
{

    error_reporting(0);
    if($employee[$j]==isset($employee_salary[$j]))
    {
        $ar=$employee[$j]+$employee_salary[$j];

        print_r($ar);
        echo "<br>";
    }
    error_reporting(0);
}

工作示例:http://phpfiddle.org/main/code/dbi8-4krn

答案 1 :(得分:0)

使用问题中给出的数组UIImageView?class Thing { var iv: UIImageView? = UIImageView() func test() { let im = UIImage() let kp = \UIImageView.image self.iv?[keyPath: kp] = im } } 的值,尝试以下解决方案。

class Thing {
    var iv: UIImageView! = UIImageView()

    func test() {
        let im = UIImage()
        let kp = \UIImageView.image
        self.iv![keyPath: kp] = im
    }
}

答案 2 :(得分:0)

我认为也将有一个简单的解决方案。因为您已经使用id键创建了employee_salary数组。所以会是这样。

Foreach($employee as $key=> $value)
{ 
     $employee[$key]['salary'] = $employee_salary[$value['employee_id']]['salary'];

 }
 Print_r($employee);die;

我认为这会帮助您。谢谢

答案 3 :(得分:0)

那应该做

    $ar= array();
    for($j=0;$j<count($employee);$j++)
    {
        $ar[$j]=$employee[$j];
        if(isset($employee_salary[$employee[$j]["employee_id"]]) && $employee_salary[$employee[$j]["employee_id"]]['employee_id'] == $employee[$j]["employee_id"] ){
            $ar[$j]=array_merge($employee[$j],$employee_salary[$employee[$j]["employee_id"]]);
        }
    }

请注意阵列键。