Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我想打印头部';根据员工的姓名选择员工的姓名_Php_Arrays - Fatal编程技术网

Php 我想打印头部';根据员工的姓名选择员工的姓名

Php 我想打印头部';根据员工的姓名选择员工的姓名,php,arrays,Php,Arrays,我想动态打印阿披实的头像,即Sumita Nath,但头像不显示。为什么会这样? 我用不同的方法尝试了很多次,但是输出没有显示出来。这是我的数据: $employee = array ( 0=> array("employee_id"=>1, "firstName"=>"Zahir", "lastName"=>"Alam", "Age"=>25, "Company"=>"Switchme", "Role"=>"Developer", "De

我想动态打印阿披实的头像,即Sumita Nath,但头像不显示。为什么会这样? 我用不同的方法尝试了很多次,但是输出没有显示出来。这是我的数据:

   $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")
    )
    );
这就是我尝试过的:

$name="Abhishek";
echo "The Head of ";
echo $name;
echo " is ";
$key = array_search($name, array_column($employee, 'Head','Name'));
//print $employee[7]["Head"echo array_]["Name"];
echo $key;

您不能在
Head
数组中搜索员工姓名,您需要搜索
firstName
列,然后检查该员工是否有
Head
数组,如果有,则从该数组中输出
name
值:

$name="Abhishek";
echo "The Head of $name is ";
$key = array_search($name, array_column($employee, 'firstName'));
if ($key !== false && isset($employee[$key]['Head'], $employee[$key]['Head']['Name'])) {
    echo $employee[$key]['Head']['Name'];
}
else {
    echo "nobody";
}
输出:

The Head of Abhishek is Sumita Nath

您无法在
Head
数组中搜索员工姓名,您需要搜索
firstName
列,然后检查该员工是否有
Head
数组,如果有,则从该数组中输出
name
值:

$name="Abhishek";
echo "The Head of $name is ";
$key = array_search($name, array_column($employee, 'firstName'));
if ($key !== false && isset($employee[$key]['Head'], $employee[$key]['Head']['Name'])) {
    echo $employee[$key]['Head']['Name'];
}
else {
    echo "nobody";
}
输出:

The Head of Abhishek is Sumita Nath

@Clarke您的
$employee
数组或
$name
中有一个拼写错误,一个是
Abhisek
,另一个是
Abhishek
,我在我的演示中更正了这一点。是的,我也看到了。谢谢你的支持help@Clarke如果这确实回答了你的问题,请考虑将答案标记为“接受”。请参见@Clarke您的
$employee
数组或
$name
中有一个拼写错误,一个是
阿披实
,另一个是
阿披实
,我在我的演示中更正了这一点。是的,我也看到了。谢谢你的支持help@Clarke如果这确实回答了你的问题,请考虑将答案标记为“接受”。看见