我有2个数组,一个数组包含员工信息,第二个数组包含员工销售。
对于“员工信息”数组,它包含ID和名称,而“员工销售”数组中包含ID和总销售额。
我尝试显示这2个数组,但是当我尝试仅显示2个数据时。
虽然我需要没有销售的ID也要显示。
我的代码:
<?php
$employees = [
$i = [
"id" => "1",
"name" => "Aliff",
],
$p = [
"id" => "2",
"name" => "Aina",
],
$a = [
"id" => "3",
"name" => "Ana",
]
];
$employee_sales = [
$i = [
"id" => "1",
"amount" => "500",
],
$p = [
"id" => "3",
"amount" => "300",
],
];
?>
<!-- Next Table --->
<?php
// echo '<pre>';
// var_dump($employees);
// var_dump($employee_sales);
foreach ($employees as $employee) {
foreach ($employee_sales as $employee_sale) {
if ($employee['id'] == $employee_sale['id']) {
?>
<table>
<tr>
<td><?= $employee['id']; ?></td>
<td><?= $employee['name']; ?></td>
<td><?= $employee_sale['amount']; ?></td>
<tr>
</table>
<?php
}
}
}
?>
<!-- Next Table --->
<br>
<br>
<br>
<?php
foreach ($employees as $employee) {
?>
<table>
<tr>
<td><?= $employee['id']; ?></td>
<td><?= $employee['name']; ?></td>
<tr>
</table>
<?php } ?>
<!-- Next Table --->
<br>
<br>
<br>
<?php
foreach ($employee_sales as $employee_sale) {
?>
<table>
<tr>
<td><?= $employee_sale['id']; ?></td>
<td><?= $employee_sale['amount']; ?></td>
<tr>
</table>
<?php } ?>
即使该员工没有任何总销售额,我也希望显示其员工编号2。
由于员工没有任何销售,想显示amount = 0
。
答案 0 :(得分:0)
添加一个变量,该变量指示是否为该员工打印了任何销售记录。如果没有,则打印销售空的行。
foreach ($employees as $employee) {
$sales_found = false;
foreach ($employee_sales as $employee_sale) {
if ($employee['id'] == $employee_sale['id']) {
$sales_found = true;
?>
<table>
<tr>
<td><?= $employee['id']; ?></td>
<td><?= $employee['name']; ?></td>
<td><?= $employee_sale['amount']; ?></td>
<tr>
</table>
<?php }
}
if (!$sales_found) { ?>
<table>
<tr>
<td><?= $employee['id']; ?></td>
<td><?= $employee['name']; ?></td>
<td>0</td>
<tr>
</table>
<?php
}
} ?>
答案 1 :(得分:0)
请选中此一项。
<?php
$employees = array(1=> array('id' => "1" , 'name' => 'Aliff') ,2 => array('id' => "2" , 'name' => 'Aina'),3 => array('id' => "3" , 'name' => 'Ana') );
$employee_sales = array(1 => array('id' => "1" , 'amount' => '500') ,2 => array('id' => "3" , 'amount' => '300'));
$data = array();
foreach ($employees as $key => $value) {
$data[$value['id']] = $value['name'];
}
$data1 = array();
foreach ($employee_sales as $key1 => $value1) {
$data1[$value1['id']] = $value1['amount'];
}
?>
<table>
<?php foreach ($data as $key => $value) {?>
<?php if(isset($data1[$key])){?>
<tr>
<td><?= $key; ?></td>
<td><?= $value; ?></td>
<td><?= $data1[$key]; ?></td>
<tr>
<?php }else{?>
<tr>
<td><?= $key; ?></td>
<td><?= $value; ?></td>
<td>0</td>
<tr>
<?php } ?>
<?php } ?>
</table>
答案 2 :(得分:0)
首先,您可以重新索引阵列,以更轻松地按员工ID访问数据。另外,您可以在一个表中显示所有数据:
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Amount</td>
</tr>
<?php
$employees = array_combine(array_column($employees, 'id'), $employees);
$employee_sales = array_combine(array_column($employee_sales, 'id'), $employee_sales);
foreach ($employees as $employeeId => $employee) {
?>
<tr>
<td><?= $employee['id']; ?></td>
<td><?= $employee['name']; ?></td>
<td><?= (isset($employee_sales[$employeeId]) ? $employee_sales[$employeeId]['amount'] : 0) ?></td>
<tr>
<?php
}
?>
</table>
答案 3 :(得分:0)
<?php
$employees = [
[
"id" => "1",
"name" => "Aliff",
],
[
"id" => "2",
"name" => "Aina",
],
[
"id" => "3",
"name" => "Ana",
]
];
$employee_sales = [
[
"id" => "1",
"amount" => "500",
],
[
"id" => "3",
"amount" => "300",
],
];
$id_sales = array_column($employee_sales, 'amount', 'id');
?>
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>sales</th>
</tr>
</thead>
<tbody>
<?php foreach ($employees as $employee) { ?>
<tr>
<td><?= $employee['id']; ?></td>
<td><?= $employee['name']; ?></td>
<td><?=
isset($id_sales[$employee['id']])
? $id_sales[$employee['id']]
: '0' ?></td>
</tr>
<?php } ?>
</tbody>
</table>
答案 4 :(得分:0)
您可以在代码中使用以下循环。
<?php
foreach ($employees as $employee) {
?>
<table>
<tr>
<td><?php echo $employee['id']; ?></td>
<td><?php echo $employee['name']; ?></td>
<td>
<?php
$key = array_search($employee['id'], array_column($employee_sales, 'id'));
echo ( $key !== false ) ? $employee_sales[$key]['amount'] : 0;
?>
</td>
<tr>
</table>
<?php
} ?>
array_search用于在employee_sales数组中查找匹配的employee_id的索引,该索引用于获取金额。 如果找不到,则返回0