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

时间:2018-11-10 19:17:05

标签: 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 ++;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

使用:

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

代替:

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