首页 > 解决方案 > 为什么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 循环之后,我只会看到第二个数组,这意味着第一个数组被覆盖了。我没有看到这种情况发生在哪里,但对我来说似乎所有结果都应该添加到数组中。

标签: phparrays

解决方案


您的代码应该按照您的描述工作,因为您正在像这样在循环中重新分配数组变量

$resulttrans = array_column($result, "transaction_id");

如果您需要在同一个数组中获取所有结果值,则需要将其附加到数组中。你可以通过像这样将新结果合并到你的数组变量中来做到这一点

$resulttrans = array_merge($resulttrans, array_column($result, "transaction_id"));

推荐阅读