Php: when array_push encounters static

Source: Internet
Author: User
{Code ...} if staticarray_push is used, the value will not be repeated. In general, if array_push has the same push value, the duplicate value will appear. why is there no repeated value after static is used?
public function find_children_cat($cat_id, $data)    {        static $tem=array();        foreach ($data as $val)        {            if ( $val['parent_id'] == $cat_id )            {                array_push($tem, $val['cat_id']);                $this->find_children_cat($val['cat_id'], $data);            }        }        return $tem;    }

If the value of static array_push is used, there will be no repeated values. In general, if the value of array_push is the same, there will be repeated values;
Why is there no repeated value after static is used?

Reply content:
public function find_children_cat($cat_id, $data)    {        static $tem=array();        foreach ($data as $val)        {            if ( $val['parent_id'] == $cat_id )            {                array_push($tem, $val['cat_id']);                $this->find_children_cat($val['cat_id'], $data);            }        }        return $tem;    }

If the value of static array_push is used, there will be no repeated values. In general, if the value of array_push is the same, there will be repeated values;
Why is there no repeated value after static is used?

The code does not check whether there are repeated values. Normally, the internal variables of a function are released after the function is completed, but when you set the variablestaitcThe static variable exists in the entire function scope. The value of this variable is retained every time you call a function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.