[英]Dynamic accessing of the php list
我在數據庫employee(Ename,id,manager_id)和manager(name,id)中有2個表。1個manager在他之下有多個雇員。 第一行提取一位經理下的所有雇員,並創建一個列表。 現在,我希望從檢索到的員工ID中提取每個員工姓名的名稱。 如何訪問列表的每個元素? 這是我嘗試過的並引發錯誤
$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');
}
通過查看文檔 (向下滾動一點,您會發現一個名為pluck
的方法。這將返回一個包含給定列的所有值的數組。
在您的情況下,這將是:
$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');
// This will return the following array:
['Kevin', 'Tom', 'Tina', ...]
Laravel采摘方法,例如從數據中獲取名稱
$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');
第二個使用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.