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

标签 php mysql

我有一个 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_fields 包含以下数组,

[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->_connection_data->公式包含此,

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

当我在此循环之外运行函数 ($defVal->function_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 ++;
            }
        }
    }

最佳答案

使用:

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

而不是:

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

关于php - foreach 循环中的函数在保存到数组时复制返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53242539/

相关文章:

javascript - 在 zip 中从 s3 下载多个文件的正确方法?

php - Symfony2 在路由中使用默认语言环境(一种语言的一个 URL)

php - HTML格式的mysql数据库?

mysql - 如何在 MySQL 中加入第一个有序匹配?

MySQL - ORDER BY Rand() 总是给出相同的结果

Php Mysql 只回显一半结果,有时甚至没有

php - 如何使用 ZipArchive 重命名文件夹?

javascript - 通过ajax对Mysql表进行排序

mysql - 如何让所有孙子处于一对多关系 HQL 中?

javascript - 选择项目时如何使用ajax将数据获取到输入字段