首页 > 解决方案 > 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.

标签: phpmysqlyii2

解决方案


$this->context->dataProvider=array();


推荐阅读