foreach循环中的函数,在保存到数组时复制返回值

guykilcj  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(263)

我有一个foreach循环运行一个脚本,该脚本只调用一个数据库并将返回的数组存储到另一个数组。下面的脚本,

foreach( $default_fields as $defKey => $defVal ) {
        if(in_array( $defVal->display_name, $this->_connection_data->formula )) {
            $endpoints[$defVal->display_name] = $this->$addon_field->{$defVal->function_name}($addon_guid);
        }
        $display_name[] = $defVal->display_name;
    }

$default\u字段包含以下数组,

[0] => stdClass Object
    (
        [display_name] => Other Income
        [function_name] => otherIncome
    )

[1] => stdClass Object
    (
        [display_name] => Income
        [function_name] => income
    )

[2] => stdClass Object
    (
        [display_name] => Current Liabilities
        [function_name] => currentLiabilities
    )

[3] => stdClass Object
    (
        [display_name] => Non Current Liabilities
        [function_name] => nonCurrentLiabilities
    )

[4] => stdClass Object
    (
        [display_name] => Current Assets
        [function_name] => currentAssets
    )

[5] => stdClass Object
    (
        [display_name] => Non Current Assets
        [function_name] => nonCurrentAssets
    )

[6] => stdClass Object
    (
        [display_name] => Equity
        [function_name] => equity
    )

[7] => stdClass Object
    (
        [display_name] => Cost of Sales
        [function_name] => costOfSales
    )

[8] => stdClass Object
    (
        [display_name] => Expenses
        [function_name] => expenses
    )

[9] => stdClass Object
    (
        [display_name] => Other Expenses
        [function_name] => otherExpenses
    )

[10] => stdClass Object
    (
        [display_name] => Closing Bank
        [function_name] => closingBalanceBankAccounts
    )

$this->\u connection\u data->公式包含这个,

[0] => (
[1] => Income
[2] => +
[3] => Other Income
[4] => )
[5] => -
[6] => Cost of Sales

当我在这个循环之外运行函数($deffal->function\u name)时,它们返回正确的值,即一个数组,每个数组包含12个键,0-11。当循环按原样运行时,由于我不知道或不理解的原因,生成的$endpoints数组如下所示,

Array
   (
    [Other Income] => Array
      (
        [0] => function 1 results
        [1] => function 1 results
        [2] => function 1 results
        [3] => function 1 results
        [4] => function 1 results
        [5] => function 1 results
        [6] => function 1 results
        [7] => function 1 results
        [8] => function 1 results
        [9] => function 1 results
        [10] => function 1 results
        [11] => function 1 results
       )
     )
   [Income] => Array
     (
        [0] => function 1 results
        [1] => function 1 results
        [2] => function 1 results
        [3] => function 1 results
        [4] => function 1 results
        [5] => function 1 results
        [6] => function 1 results
        [7] => function 1 results
        [8] => function 1 results
        [9] => function 1 results
        [10] => function 1 results
        [11] => function 1 results
        [12] => function 2 results
        [13] => function 2 results
        [14] => function 2 results
        [15] => function 2 results
        [16] => function 2 results
        [17] => function 2 results
        [18] => function 2 results
        [19] => function 2 results
        [20] => function 2 results
        [21] => function 2 results
        [22] => function 2 results
        [23] => function 2 results
        ......

我想返回$endpoints数组,其中每个数组只包含在返回结果时调用的函数,如下所示。。。

Array
   (
    [Other Income] => Array
      (
        [0] => function 1 results
        [1] => function 1 results
        [2] => function 1 results
        [3] => function 1 results
        [4] => function 1 results
        [5] => function 1 results
        [6] => function 1 results
        [7] => function 1 results
        [8] => function 1 results
        [9] => function 1 results
        [10] => function 1 results
        [11] => function 1 results
       )
     )
   [Income] => Array
     (
        [0] => function 2 results
        [1] => function 2 results
        [2] => function 2 results
        [3] => function 2 results
        [4] => function 2 results
        [5] => function 2 results
        [6] => function 2 results
        [7] => function 2 results
        [8] => function 2 results
        [9] => function 2 results
        [10] => function 2 results
        [11] => function 2 results
        ......

有人知道我在这个过程中哪里出错了吗?任何帮助都将不胜感激。
已按要求添加函数体。。。

$data = $this->_dbuser->query('SELECT account,type,month_1,month_2,month_3,month_4,month_5,month_6,month_7,month_8,month_9,month_10,month_11,month_12 FROM db_calculate WHERE integration_guid = ?',array($guid));
    if (!empty($data->results())) {
        $data = $data->results();

        $dates = $this->_dbuser->query('SELECT month_1,month_2,month_3,month_4,month_5,month_6,month_7,month_8,month_9,month_10,month_11,month_12 FROM db_calculate_months WHERE integration_guid = ? ',array($guid));
        if ($dates->results()) {
            $dates = $dates->results()[0];

            $array = array(); $count = 0;
            foreach ($data as $row) {
                for ($start = 1; $start <= 12; $start ++) {
                    $holder = 'month_'.$start;
                    $array[$count]['account'] = $row->account;
                    $array[$count]['type'] = $row->type;
                    $array[$count]['total'] = $row->$holder;
                    $array[$count]['to_date'] = $dates->$holder;
                    $count ++;
                }
            }

        }
    }

    $filter = array(); $count = 0;
    foreach ($array as $arr) {
        if ($arr['type'] == "Income") {
            $filter[$count]['account'] = $arr['account'];
            $filter[$count]['total'] = $arr['total'];
            $filter[$count]['to_date'] = $arr['to_date'];
            $count ++;
        }
    }

    $results = array($filter[0]); $count = 1;
    foreach ($filter as $key => $value) {
        if ($key > 0) {
            $pos = array_search($value['to_date'],array_column($results, 'to_date'));
            if (in_array($value['to_date'],array_column($results,'to_date'))) {
                $results[$pos]['total'] = $results[$pos]['total'] + $value['total'];
                $count ++;
            } else {
                $results[$count] = $value;
                $count ++;
            }
        }
    }
bmp9r5qi

bmp9r5qi1#

用途:

$endpoints[$defVal->display_name][] = $this->$addon_field->{$defVal->function_name}($addon_guid);

而不是:

$endpoints[$defVal->display_name] = $this->$addon_field->{$defVal->function_name}($addon_guid);

相关问题