php - 为什么while循环创建/覆盖2个单独的数组
问题描述
我有以下代码在 while 循环的第二次通过时覆盖我的数组。
这是我的代码:
<?php
require '../vendor/autoload.php';
require_once 'constants/constants.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
require('includes/application_top.php');
define("AUTHORIZENET_LOG_FILE", "phplog");
function getUnsettledTransactionList()
{
//get orders that are in the exp status
$orders_pending_query = tep_db_query("select orders_id as invoice_number from " . TABLE_ORDERS . " where orders_status = '14' order by invoice_number");
$orders_pending = array();
while ($row = mysqli_fetch_array($orders_pending_query, MYSQLI_ASSOC)) {
$orders_pending[] = $row;
}
/* Create a merchantAuthenticationType object with authentication details
retrieved from the constants file */
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(\SampleCodeConstants::MERCHANT_LOGIN_ID);
$merchantAuthentication->setTransactionKey(\SampleCodeConstants::MERCHANT_TRANSACTION_KEY);
// Set the transaction's refId
$refId = 'ref' . time();
$pagenum = 1;
do {
$request = new AnetAPI\GetUnsettledTransactionListRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$paging = new AnetAPI\PagingType;
$paging->setLimit("1000");
$paging->setOffset($pagenum);
$request->setPaging($paging);
$controller = new AnetController\GetUnsettledTransactionListController($request);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);
$transactionArray = array();
$resulttrans = array();
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
if (null != $response->getTransactions()) {
foreach ($response->getTransactions() as $tx) {
$transactionArray[] = array(
'transaction_id' => $tx->getTransId(),
'invoice_number' => $tx->getInvoiceNumber()
);
// echo "TransactionID: " . $tx->getTransId() . "order ID:" . $tx->getInvoiceNumber() . "Amount:" . $tx->getSettleAmount() . "<br/>";
}
$invoiceNumbers = array_column($orders_pending, "invoice_number");
$result = array_filter($transactionArray, function ($x) use ($invoiceNumbers) {
return in_array($x["invoice_number"], $invoiceNumbers);
});
$resulttrans = array_column($result, "transaction_id");
} else {
echo "No unsettled transactions for the merchant." . "\n";
}
} else {
echo "ERROR : Invalid response\n";
$errorMessages = $response->getMessages()->getMessage();
echo "Response : " . $errorMessages[0]->getCode() . " " . $errorMessages[0]->getText() . "\n";
}
$numResults = (int) $response->getTotalNumInResultSet();
$pagenum++;
print_r($resulttrans);
} while ($numResults === 1000);
return $resulttrans;
}
getUnsettledTransactionList();
?>
print_r($resulttrans); 实际上是打印 2 个单独的数组,而不是我想要的 1 个数组。
如果我将 print_r($resulttrans) 移动到 while 循环之后,我只会看到第二个数组,这意味着第一个数组被覆盖了。我没有看到这种情况发生在哪里,但对我来说似乎所有结果都应该添加到数组中。
解决方案
您的代码应该按照您的描述工作,因为您正在像这样在循环中重新分配数组变量
$resulttrans = array_column($result, "transaction_id");
如果您需要在同一个数组中获取所有结果值,则需要将其附加到数组中。你可以通过像这样将新结果合并到你的数组变量中来做到这一点
$resulttrans = array_merge($resulttrans, array_column($result, "transaction_id"));
推荐阅读
- r - 在匹配 R 中的条件的字符串数组中对值进行分组
- algorithm - 哈希映射中的单独链接
- python-3.x - 使用 REGEX 从 Python 中的列中删除一段文本字符串
- php - .htaccess 将目录中的任何文件重定向到同一目录的 index.php
- hyper-v - Hyper-V 机器版本
- string - 适用于 Linux 的 Windows 子系统 (WSL) 中的 C++ 字符串连接正在删除第一个字符串
- javascript - Collection#find:传递一个函数而不是错误
- python - Python Pandas 数据框比较时间序列数据中的行
- three.js - 我如何在 webgl 中捕获这种类型的#define
- bash - 读取 N 个字符或按回车键退出