简体   繁体   English

动态访问PHP列表

[英]Dynamic accessing of the php list

I have 2 tables in the database employee(Ename,id,manager_id) and manager(name,id).1 manager has multiple employees under him. 我在数据库employee(Ename,id,manager_id)和manager(name,id)中有2个表。1个manager在他之下有多个雇员。 The first line extracts all the employees under 1 manager and creates a list of it. 第一行提取一位经理下的所有雇员,并创建一个列表。 Now, I wish to extract the name of each employee name from the retrieved employee id. 现在,我希望从检索到的员工ID中提取每个员工姓名的名称。 How do i access each element of the list? 如何访问列表的每个元素? This is what i have tried and it throws errors 这是我尝试过的并引发错误

$emplyeeId=DB::table('employee')->where('manager_id', $givenManagerId)->lists('id');  
for ($i=0;$i<listCount;$i++)
{
    $Ename = DB::table('employee')->where('id', $emplyeeId($i))-> value('Ename');
}

By looking at the docs (and scroll down a little bit, you find a method named pluck . This will return an array with all the values of the given column. 通过查看文档 (向下滚动一点,您会发现一个名为pluck的方法。这将返回一个包含给定列的所有值的数组。

In your case this would be: 在您的情况下,这将是:

$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');

// This will return the following array:
['Kevin', 'Tom', 'Tina', ...]

Laravel pluck method ,for example get just name from data Laravel采摘方法,例如从数据中获取名称

$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');


Second method with array_filter 第二个使用array_filter的方法


$names = DB::table('employee')->where('manager_id', $givenManagerId)->get()->toArray();


$justNames=array_filter($names, function ($ar) { return $ar['Ename']; });


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM