function in foreach loop duplicating returned values when saving to an array











up vote
0
down vote

favorite












I have a foreach loop running a script that simply calls a database and stores the returned array to another array. The script below,



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 contains the below array,



[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->formula contains this,



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


When I run the functions ($defVal->function_name) outside of this loop, they return the correct values that are an array that contain 12 keys each, 0-11. When the loop runs as it is, for reasons I do not know or understand, The resulting $endpoints array looks something like the below,



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
......


I am wanting to return the $endpoints array with each array in it to only contain the function that was called at the times results, like so...



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
......


Has anyone got an idea on where I have gone wrong in this process ?
Any help would be greatly appreciated.



Added the function body as requested...



$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 ++;
}
}
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a foreach loop running a script that simply calls a database and stores the returned array to another array. The script below,



    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 contains the below array,



    [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->formula contains this,



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


    When I run the functions ($defVal->function_name) outside of this loop, they return the correct values that are an array that contain 12 keys each, 0-11. When the loop runs as it is, for reasons I do not know or understand, The resulting $endpoints array looks something like the below,



    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
    ......


    I am wanting to return the $endpoints array with each array in it to only contain the function that was called at the times results, like so...



    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
    ......


    Has anyone got an idea on where I have gone wrong in this process ?
    Any help would be greatly appreciated.



    Added the function body as requested...



    $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 ++;
    }
    }
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a foreach loop running a script that simply calls a database and stores the returned array to another array. The script below,



      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 contains the below array,



      [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->formula contains this,



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


      When I run the functions ($defVal->function_name) outside of this loop, they return the correct values that are an array that contain 12 keys each, 0-11. When the loop runs as it is, for reasons I do not know or understand, The resulting $endpoints array looks something like the below,



      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
      ......


      I am wanting to return the $endpoints array with each array in it to only contain the function that was called at the times results, like so...



      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
      ......


      Has anyone got an idea on where I have gone wrong in this process ?
      Any help would be greatly appreciated.



      Added the function body as requested...



      $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 ++;
      }
      }
      }









      share|improve this question















      I have a foreach loop running a script that simply calls a database and stores the returned array to another array. The script below,



      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 contains the below array,



      [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->formula contains this,



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


      When I run the functions ($defVal->function_name) outside of this loop, they return the correct values that are an array that contain 12 keys each, 0-11. When the loop runs as it is, for reasons I do not know or understand, The resulting $endpoints array looks something like the below,



      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
      ......


      I am wanting to return the $endpoints array with each array in it to only contain the function that was called at the times results, like so...



      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
      ......


      Has anyone got an idea on where I have gone wrong in this process ?
      Any help would be greatly appreciated.



      Added the function body as requested...



      $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 ++;
      }
      }
      }






      php mysql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 20:28

























      asked Nov 10 at 19:17









      5abre

      166




      166
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          use :



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


          instead of :



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





          share|improve this answer





















          • or might be $endpoints[$defVal->display_name][$addon_guid] = $this->$addon_field->{$defVal->function_name}($addon_guid);
            – Zuko
            Nov 10 at 19:34










          • Thanks FatemehNB, but this only adds another array depth, it does not stop the duplication of the function results when saving to the array.
            – 5abre
            Nov 10 at 19:36










          • maybe problem is in your functions body? is it possible to put them in your question?
            – FatemehNB
            Nov 10 at 19:43










          • I have added the functions body, the only difference between the functions is the data filtered in the $filter array. Thank you for your help.
            – 5abre
            Nov 10 at 20:29










          • I suggest you to check body of your functions to be asuured that youdid not put duplicate if condition for filter array.
            – FatemehNB
            Nov 10 at 21:52











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242539%2ffunction-in-foreach-loop-duplicating-returned-values-when-saving-to-an-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          use :



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


          instead of :



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





          share|improve this answer





















          • or might be $endpoints[$defVal->display_name][$addon_guid] = $this->$addon_field->{$defVal->function_name}($addon_guid);
            – Zuko
            Nov 10 at 19:34










          • Thanks FatemehNB, but this only adds another array depth, it does not stop the duplication of the function results when saving to the array.
            – 5abre
            Nov 10 at 19:36










          • maybe problem is in your functions body? is it possible to put them in your question?
            – FatemehNB
            Nov 10 at 19:43










          • I have added the functions body, the only difference between the functions is the data filtered in the $filter array. Thank you for your help.
            – 5abre
            Nov 10 at 20:29










          • I suggest you to check body of your functions to be asuured that youdid not put duplicate if condition for filter array.
            – FatemehNB
            Nov 10 at 21:52















          up vote
          0
          down vote













          use :



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


          instead of :



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





          share|improve this answer





















          • or might be $endpoints[$defVal->display_name][$addon_guid] = $this->$addon_field->{$defVal->function_name}($addon_guid);
            – Zuko
            Nov 10 at 19:34










          • Thanks FatemehNB, but this only adds another array depth, it does not stop the duplication of the function results when saving to the array.
            – 5abre
            Nov 10 at 19:36










          • maybe problem is in your functions body? is it possible to put them in your question?
            – FatemehNB
            Nov 10 at 19:43










          • I have added the functions body, the only difference between the functions is the data filtered in the $filter array. Thank you for your help.
            – 5abre
            Nov 10 at 20:29










          • I suggest you to check body of your functions to be asuured that youdid not put duplicate if condition for filter array.
            – FatemehNB
            Nov 10 at 21:52













          up vote
          0
          down vote










          up vote
          0
          down vote









          use :



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


          instead of :



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





          share|improve this answer












          use :



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


          instead of :



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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 19:23









          FatemehNB

          22626




          22626












          • or might be $endpoints[$defVal->display_name][$addon_guid] = $this->$addon_field->{$defVal->function_name}($addon_guid);
            – Zuko
            Nov 10 at 19:34










          • Thanks FatemehNB, but this only adds another array depth, it does not stop the duplication of the function results when saving to the array.
            – 5abre
            Nov 10 at 19:36










          • maybe problem is in your functions body? is it possible to put them in your question?
            – FatemehNB
            Nov 10 at 19:43










          • I have added the functions body, the only difference between the functions is the data filtered in the $filter array. Thank you for your help.
            – 5abre
            Nov 10 at 20:29










          • I suggest you to check body of your functions to be asuured that youdid not put duplicate if condition for filter array.
            – FatemehNB
            Nov 10 at 21:52


















          • or might be $endpoints[$defVal->display_name][$addon_guid] = $this->$addon_field->{$defVal->function_name}($addon_guid);
            – Zuko
            Nov 10 at 19:34










          • Thanks FatemehNB, but this only adds another array depth, it does not stop the duplication of the function results when saving to the array.
            – 5abre
            Nov 10 at 19:36










          • maybe problem is in your functions body? is it possible to put them in your question?
            – FatemehNB
            Nov 10 at 19:43










          • I have added the functions body, the only difference between the functions is the data filtered in the $filter array. Thank you for your help.
            – 5abre
            Nov 10 at 20:29










          • I suggest you to check body of your functions to be asuured that youdid not put duplicate if condition for filter array.
            – FatemehNB
            Nov 10 at 21:52
















          or might be $endpoints[$defVal->display_name][$addon_guid] = $this->$addon_field->{$defVal->function_name}($addon_guid);
          – Zuko
          Nov 10 at 19:34




          or might be $endpoints[$defVal->display_name][$addon_guid] = $this->$addon_field->{$defVal->function_name}($addon_guid);
          – Zuko
          Nov 10 at 19:34












          Thanks FatemehNB, but this only adds another array depth, it does not stop the duplication of the function results when saving to the array.
          – 5abre
          Nov 10 at 19:36




          Thanks FatemehNB, but this only adds another array depth, it does not stop the duplication of the function results when saving to the array.
          – 5abre
          Nov 10 at 19:36












          maybe problem is in your functions body? is it possible to put them in your question?
          – FatemehNB
          Nov 10 at 19:43




          maybe problem is in your functions body? is it possible to put them in your question?
          – FatemehNB
          Nov 10 at 19:43












          I have added the functions body, the only difference between the functions is the data filtered in the $filter array. Thank you for your help.
          – 5abre
          Nov 10 at 20:29




          I have added the functions body, the only difference between the functions is the data filtered in the $filter array. Thank you for your help.
          – 5abre
          Nov 10 at 20:29












          I suggest you to check body of your functions to be asuured that youdid not put duplicate if condition for filter array.
          – FatemehNB
          Nov 10 at 21:52




          I suggest you to check body of your functions to be asuured that youdid not put duplicate if condition for filter array.
          – FatemehNB
          Nov 10 at 21:52


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242539%2ffunction-in-foreach-loop-duplicating-returned-values-when-saving-to-an-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma