Home > php教程 > php手册 > body text

让array_column()函数兼容低版本PHP

WBOY
Release: 2016-06-07 11:43:51
Original
1557 people have browsed it

array_column 用于获取二维数组中的元素(PHP 5.5新增函数),但我们有时候需要在低版本的PHP环境中使用,只要做一个小的改造即可...
自定义一个函数i_array_column(),写到TP的Common中~~function i_array_column($input, $columnKey, $indexKey=null){<br>     if(!function_exists('array_column')){ <br>         $columnKeyIsNumber  = (is_numeric($columnKey))?true:false; <br>         $indexKeyIsNull            = (is_null($indexKey))?true :false; <br>         $indexKeyIsNumber     = (is_numeric($indexKey))?true:false; <br>         $result                         = array(); <br>         foreach((array)$input as $key=>$row){ <br>             if($columnKeyIsNumber){ <br>                 $tmp= array_slice($row, $columnKey, 1); <br>                 $tmp= (is_array($tmp) && !empty($tmp))?current($tmp):null; <br>             }else{ <br>                 $tmp= isset($row[$columnKey])?$row[$columnKey]:null; <br>             } <br>             if(!$indexKeyIsNull){ <br>                 if($indexKeyIsNumber){ <br>                   $key = array_slice($row, $indexKey, 1); <br>                   $key = (is_array($key) && !empty($key))?current($key):null; <br>                   $key = is_null($key)?0:$key; <br>                 }else{ <br>                   $key = isset($row[$indexKey])?$row[$indexKey]:0; <br>                 } <br>             } <br>             $result[$key] = $tmp; <br>         } <br>         return $result; <br>     }else{<br>         return array_column($input, $columnKey, $indexKey);<br>     }<br> }在需要用到array_column()函数的地方,只要调用i_array_column()即可,在低版本的PHP环境中,同样可以享受array_column()带来的便捷与快感~~

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!