Question

here is an example.

public function jscss($module)
    {
    if (is_array($module))
    {
        foreach ($module as $val)
        {
            $this->jscss($val);
        }
        return;
    }

    if ( ! $module)
    {
        return;
    }

    if(in_array($module, $this->_loaded_jscss_modules)) return;

    array_push($this->_loaded_jscss_modules, $module);
    $this->_loaded_jscss_modules = array_flip(array_flip($this->_loaded_jscss_modules)); // <-- Right this part where i am talking about

    $aSrc = array();

    //css
    foreach($this->_jscss_modules as $key => $val)
    {
        if(in_array($key, $this->_loaded_jscss_modules) && isset($val['css']) )
            foreach($val['css'] as $css)
                $aSrc[] = '<link rel="stylesheet" href="'.$css.'" type="text/css" />';
    }

    //js
    foreach($this->_jscss_modules as $key => $val)
    {
        if(in_array($key, $this->_loaded_jscss_modules) && isset($val['js']) )
            foreach($val['js'] as $js)
                $aSrc[] = '<script type="text/javascript" src="'.$js.'"></script>';
    }

    $vars['jscss_src'] = "\n".implode("\n",$aSrc)."\n";
    $this->vars($vars);

}

You can see the array_flip used twice where i have commented. Why using function array_flip twice?

Was it helpful?

Solution

its possible used as a filtering mechanism. based on the documentation. for deduplication and removal of none alphanumeric values.

Note that the values of trans need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be included in the result.

If a value has several occurrences, the latest key will be used as its value, and all others will be lost

OTHER TIPS

array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys.

Note that the values of trans need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be included in the result.

If a value has several occurrences, the latest key will be used as its value, and all others will be lost. So here it removes the duplicate values, When you do flip twice, first it removes the duplicate values and exchanges key and values in second flip it again exchanges the value and key (So becomes same as original array except it removes all dupliacte values)

Ref : http://php.net/manual/en/function.array-flip.phpf:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top