这是我的问题,我有两个数组,一个是多维数组,第二个是单个数组,这些数组显示在两个表中。多数组显示在表1中,单数组显示在表2中。我要做的是计算每个职位的员工人数。
<?php include_once('fake_db.php'); ?>
<h3>Department List</h3>
<table class="table table-condensed table-striped table-bordered table-hover no-margin">
<thead>
<tr>
<td>Name</td><td>Number of Employees</td><td>Action</td>
</tr>
</thead>
<?php foreach($positions as $key => $value){ ?>
<tr>
<td><?= $value; ?></td>
<td></td>
<td><a href="profile.php?id=<?= $key; ?>">View Employee List</a></td>
</tr>
<?php } ?>
</table>
fake_db.php
$employees = array
(
array("name" => 'Jason Alipala', "employee_id" => 'G1001-05', "position" => 1),
array("name" => 'Bryann Revina', "employee_id" => 'G1009-03', "position" => 2),
array("name" => 'Jeniel Mangahis', "employee_id" => 'G1009-04', "position" => 2),
array("name" => 'Arjay Bussala', "employee_id" => 'G1009-05', "position" => 3),
array("name" => 'Ronnel Ines', "employee_id" => 'G1002-06', "position" => 3)
);
$positions = array(1 => 'TL', 2 => 'Programmer', 3 => 'Converter');
答案 0 :(得分:2)
要计算每个职位的员工数量,可以在position
数组的employees
列上使用array_count_values
。例如:
$emp_positions = array_count_values(array_column($employees, 'position'));
foreach ($positions as $key => $value) {
echo $positions[$key] . ' : ' . $emp_positions[$key] . ' employees' . PHP_EOL;
}
输出:
TL : 1 employees
Programmer : 2 employees
Converter : 2 employees