为什么我的条件语句没有在 foreach 循环中执行多次? PHP
Why is my conditional statement not executing multiple times in a foreach loop? PHP
我正在尝试打印出与客户列表匹配的订单信息,具体取决于订单上的客户 ID 是否与客户的客户 ID 匹配。客户和订单都存储在数组中,到目前为止,我在执行订单 table 和打印信息时没有遇到任何问题。我遇到的问题是只有一个订单正在为客户打印,而一些客户有多个订单应该打印。我使用 foreach 循环遍历与订单 ID 匹配的订单到请求的客户 ID,该 ID 来自查询字符串。当我知道至少有 1 个正在打印时,我不知道为什么我的 if 语句没有为多个订单打印。
这是我的客户 table 的代码,它在返回带有客户 ID 的查询字符串时执行。底部的 foreach 循环是我期望打印所有匹配订单的地方,但是当我在某些情况下期望多个订单时它只给我一个订单。如果需要任何其他信息,我可以提供。谢谢
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if (isset($_GET['customer'])) {
$requestedCustomer = $customers[$_GET['customer']];
$orders = readOrders('orders.txt');
你检查过为什么会发生这种情况吗?一个 ID 不能在同一个数组中多次使用,因此如果您解析具有相同 ID 的多行并将它们写入数组 使用相同的 ID,您将覆盖之前的订单。只能访问具有该 ID 的最后一个订单。通过比较解析的订单数组和包含您的订单的文件,应该很容易看到。
这就是为什么您通常使用 唯一 订单 ID,并将用户 ID 存储在该订单中的原因
我认为你的问题在 readOrders
。你似乎每次都用 $orders[$order['id']] = $order;
破坏你的数组,而不是你想要 $orders[$order['id']][] = $order;
function readOrders($filename)
{
$arr = file($filename) or die('ERROR: Cannot find file');
// Create our primary order array
$orders = [];
$delimiter = ',';
// reading in customers from file and storing in array of customers
foreach ($arr as $line) {
$splitcontents = explode($delimiter, $line);
$order = array();
$order['id'] = $splitcontents[1];
$order['isbn'] = $splitcontents[2];
$order['title'] = utf8_encode($splitcontents[3]);
$order['category'] = utf8_encode($splitcontents[4]);
// Append the order to the array,
$orders[$order['id']][] = $order;
}
return $orders;
}
然后,在您的打印循环中,您不会遍历 $orders
,而是获取由客户 ID 索引的主订单数组并获取这些特定订单。
// Get the orders for this specific customer, if they exist
$customerOrders = $orders[$requestedCustomer['id']] ?? [];
foreach ($customerOrders as $order) {
echo '<tr>';
echo '<td>' . $order['isbn'] . '</td>';
echo '<td>' . $order['title'] . '</td>';
echo '<td>' . $order['category'] . '</td>';
echo '</tr>';
}
我正在尝试打印出与客户列表匹配的订单信息,具体取决于订单上的客户 ID 是否与客户的客户 ID 匹配。客户和订单都存储在数组中,到目前为止,我在执行订单 table 和打印信息时没有遇到任何问题。我遇到的问题是只有一个订单正在为客户打印,而一些客户有多个订单应该打印。我使用 foreach 循环遍历与订单 ID 匹配的订单到请求的客户 ID,该 ID 来自查询字符串。当我知道至少有 1 个正在打印时,我不知道为什么我的 if 语句没有为多个订单打印。
这是我的客户 table 的代码,它在返回带有客户 ID 的查询字符串时执行。底部的 foreach 循环是我期望打印所有匹配订单的地方,但是当我在某些情况下期望多个订单时它只给我一个订单。如果需要任何其他信息,我可以提供。谢谢
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if (isset($_GET['customer'])) {
$requestedCustomer = $customers[$_GET['customer']];
$orders = readOrders('orders.txt');
你检查过为什么会发生这种情况吗?一个 ID 不能在同一个数组中多次使用,因此如果您解析具有相同 ID 的多行并将它们写入数组 使用相同的 ID,您将覆盖之前的订单。只能访问具有该 ID 的最后一个订单。通过比较解析的订单数组和包含您的订单的文件,应该很容易看到。
这就是为什么您通常使用 唯一 订单 ID,并将用户 ID 存储在该订单中的原因
我认为你的问题在 readOrders
。你似乎每次都用 $orders[$order['id']] = $order;
破坏你的数组,而不是你想要 $orders[$order['id']][] = $order;
function readOrders($filename)
{
$arr = file($filename) or die('ERROR: Cannot find file');
// Create our primary order array
$orders = [];
$delimiter = ',';
// reading in customers from file and storing in array of customers
foreach ($arr as $line) {
$splitcontents = explode($delimiter, $line);
$order = array();
$order['id'] = $splitcontents[1];
$order['isbn'] = $splitcontents[2];
$order['title'] = utf8_encode($splitcontents[3]);
$order['category'] = utf8_encode($splitcontents[4]);
// Append the order to the array,
$orders[$order['id']][] = $order;
}
return $orders;
}
然后,在您的打印循环中,您不会遍历 $orders
,而是获取由客户 ID 索引的主订单数组并获取这些特定订单。
// Get the orders for this specific customer, if they exist
$customerOrders = $orders[$requestedCustomer['id']] ?? [];
foreach ($customerOrders as $order) {
echo '<tr>';
echo '<td>' . $order['isbn'] . '</td>';
echo '<td>' . $order['title'] . '</td>';
echo '<td>' . $order['category'] . '</td>';
echo '</tr>';
}