php - How to unset or clear a global variable in php YII2 Recursive function?
问题描述
On My Controller
class BalanceSheetController extends Controller
{
public $dataProvider=array(); //Global for The recursuive function
public function recursive_select_transactions($ledger,$array_){
$Current_ledger = AccountGroup::find()->where(['like','slug',$ledger])->one()->id;
$get_childs = AccountGroup::find()->select('id,slug,parent_id,name')->where(['parent_id'=>$Current_ledger])->asArray()->all();
if (count($get_childs) != 0) {
foreach($get_childs as $childs ) {
$this->recursive_select_transactions($childs['slug'],$array_);
}
}
$get_head = AccountHead::find()->where(['group_id'=>$Current_ledger])->asArray()->all();
$get_head_altered=array();
foreach($get_head as $head){
$acc_slug = str_replace("-", "_", $head['slug']);
$head['transaction_slug']=$acc_slug ;
array_push($get_head_altered,$head);
}
foreach($get_head_altered as $key=>$val){
$array_[]=$val['transaction_slug'];
}
$this->dataProvider[]=$array_;
}
}
On My View
function array_flatten($array)
{
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
} else {
$result[$key] = $value;
}
}
return $result;
}
$liability = array();
$liability_details = $this->context->recursive_select_transactions('liability', $liability);
$liability_details = $this->context->dataProvider;
$liability_slugs = array_flatten($liability_details);
$asset=array();
$asset_details = $this->context->recursive_select_transactions('asset', $asset);
$asset_details = $this->context->dataProvider;
$asset_slugs = array_flatten($asset_details);
echo"<pre>";print_r($liability_slugs);echo"<pre>";
echo"<pre>";print_r($asset_slugs);echo"<pre>";
$dataProvider is the global variable.And The Resultant array,The last called have the elements of first Called.Cause the global Variable keeps
Array
(
[0] => chitty_settlement
[1] => cess_collected
[2] => hallmarking_charges_received
)
Array
(
[0] => chitty_settlement
[1] => cess_collected
[2] => hallmarking_charges_received
[3] => output_cgst_ac
[4] => output_igst_ac
[5] => output_sgst_ac
)
This first Array (libility) element remains when it called with diiferent parameter (asset). How To Clear the Elements of Global Variable before its again called with different Parameter I tried this
$liability = array();
$liability_details = $this->context->recursive_select_transactions('liability', $liability);
$liability_details = $this->context->dataProvider;
$liability_slugs = array_flatten($liability_details);
unset($this->context->dataProvider);
$asset=array();
$asset_details = $this->context->recursive_select_transactions('asset', $asset);
$asset_details = $this->context->dataProvider;
$asset_slugs = array_flatten($asset_details);
its not working.
解决方案
$this->context->dataProvider=array();
推荐阅读
- idris - 如何使用来自 Maybe 的数据作为 Vect 大小变量
- keras - Tensorflow:自定义损失函数以鼓励/限制误报/否定
- reactjs - 从 switch 语句反应返回视图
- docker - Docker 中的 Azure DevOps 代理:添加自定义功能
- haskell - 如何链接以获取餐厅名称
- c - 系统调用 read() 被阻塞
- azure-backup-vault - MARS 备份(系统状态)备份失败 - Windows server 2012
- laravel - 在 Laravel 5.8 迁移中,无法将外部列从可为空更改为不为空
- sql - 子查询应该总是选择语句吗?
- oracle - 如何通过更改计数值在 Oracle 中按组填写缺失的日期