我想从$ employee_salary数组中的$ employee数组中添加薪水键

时间:2019-04-20 19:18:39

标签: php

我想从$ employee_salary数组中的$ employee数组中添加薪水键,这两个数组都是关联的,并且salary仅根据索引来加薪。存在一些未定义偏移量的错误。我无法识别问题。错误就像未定义的偏移量。这是什么问题?

    $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++)
{

    if($employee[$j]==$employee_salary[$j])
    {
        $ar=$employee[$j]+$employee_salary[$j];
        print_r($ar);
        echo "<br>";
    }
}

3 个答案:

答案 0 :(得分:1)

使用array_column索引员工ID,并循环薪水,然后使用相同的键将值添加到员工数组中。

$employee = array_column($employee, null, "employee_id");
$employee_salary = array_column($employee_salary, "salary", "employee_id");

foreach($employee_salary as $key => $v){
    $employee[$key]['salary'] = $v;
}

var_dump($employee);

https://3v4l.org/W8sfq

答案 1 :(得分:0)

如果您可以注意到,df1df1[:, [1:20, 55:57]]都是数组,因此您不应该等于比较它们。另外,using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shooting : MonoBehaviour { [SerializeField] private Transform[] firePoints; [SerializeField] private Rigidbody projectilePrefab; [SerializeField] private float launchForce = 700f; [SerializeField] private Animator anim; [SerializeField] private bool automaticFire = false; [SerializeField] private bool slowDownEffect = false; private void Start() { anim.SetBool("Shooting", true); } public void Update() { if (isAnimationStatePlaying(anim, 0, "AIMING") == true) { if (Input.GetButtonDown("Fire1") && automaticFire == false) { if (anim.GetBool("Shooting") == true) { anim.Play("SHOOTING"); LaunchProjectile(); } }else if(Input.GetButtonDown("Fire1") && automaticFire == true){ automaticFire = true; } else { if (/*automaticFire == true &&*/Input.GetButtonDown("Fire2")) { automaticFire = true; } if (automaticFire == true ) { anim.Play("SHOOTING"); LaunchProjectile(); } } } } private void LaunchProjectile() { foreach (var firePoint in firePoints) { Rigidbody projectileInstance = Instantiate( projectilePrefab, firePoint.position, firePoint.rotation); projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce); projectileInstance.gameObject.AddComponent<BulletDestruction>().Init(); } } bool isAnimationStatePlaying(Animator anim, int animLayer, string stateName) { if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName)) return true; else return false; } } $employee[$j]数组的大小不同,这就是为什么在$employee_salary[$j]中执行$employee时出现 undefined index 错误循环。

reference一起使用嵌套的$employee_salary循环可达到所需的结果,如下所示:

$employee_salary[$j]

答案 2 :(得分:0)

您可以简单地使用array_walk

$emp_s = array_column($employee_salary, 'salary','employee_id');
array_walk($employee, function(&$v, $k) use ($emp_s){
  if(array_key_exists($k, $emp_s)){
    $v['salary'] = $emp_s[$k];
  }
});

OR

$temp        = array_column($employee_salary, 'salary','employee_id');
array_walk($employee, function(&$v, $k) use ($temp){
    $v['salary'] = array_key_exists($k, $temp) ? $temp[$k] : null;
});