如何使用Php在foreach之外获取具有特定索引的数组

My problem was to get the array employeeIds in an array outside a foreach

$jobCard

$jobCard contains an associative in my instance I get the employee_id in the array

$employeeId = array();  

foreach ($jobCards as $jobCard) {
    $employeeId[] = $jobCard['employee_id'];
}

$employees = Employee::LoadArray($employeeId); 

foreach ($employees as $employeeID => $Employee) {
    $employeeName[$employeeID] = $Employee->getName();
    Console::Log('name', $employeeName);
}

foreach ($jobCards as $jobCard) {
    Console::Log('$employeeName', $employeeName);
    $notes[] = $employeeName[$employeeID] . " 
" .$jobCard['description_notes'];
}

$detail['notes'] = implode("
", $notes);
Console::Log('display', $detail);

My understanding of your question is that you want employee names for ids combined with the description notes. If so, then your iteration logic is a bit mixed up.

A simpler way is to:

  1. get all employees
  2. iterate $jobCards and compile the note while looking up the employee name in the employees array

E.g.:

<?php
// get all employees for the collected ids
$employees = Employee::LoadArray(array_column($jobCards, 'employee_id'));

// map job cards to notes
$notes = array_map(function ($jobCard) use($employees) {
    return sprintf(
        "%s
%s",
        // access the employee name directly
        $employees[$jobCard['employee_id']]->getName(),
        $jobCard['description_notes']
    );
}, $jobCards);

$detail['notes'] = implode("
", $notes);

function reference:

array_column, array_map