首页 > 语言 > PHP > 正文

自己写的兼容低于PHP 5.5版本的array_column()函数

2024-09-04 11:43:09
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了自己写的兼容低于PHP 5.5版本的array_column()函数,array_column是PHP 5.5新增函数,有时在低版本中也可能要用到,需要的朋友可以参考下

array_column 用于获取二维数组中的元素(PHP 5.5新增函数),但我们有时候需要在低版本的PHP环境中使用…

  1. if( ! function_exists('array_column')) 
  2. { 
  3.   function array_column($input, $columnKey, $indexKey = NULL) 
  4.   { 
  5.     $columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE; 
  6.     $indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE; 
  7.     $indexKeyIsNumber = (is_numeric($indexKey)) ? TRUE : FALSE; 
  8.     $result = array(); 
  9.  
  10.     foreach ((array)$input AS $key => $row) 
  11.     {  
  12.       if ($columnKeyIsNumber) 
  13.       { 
  14.         $tmp = array_slice($row, $columnKey, 1); 
  15.         $tmp = (is_array($tmp) && !emptyempty($tmp)) ? current($tmp) : NULL; 
  16.       } 
  17.       else 
  18.       { 
  19.         $tmp = isset($row[$columnKey]) ? $row[$columnKey] : NULL; 
  20.       } 
  21.       if ( ! $indexKeyIsNull) 
  22.       { 
  23.         if ($indexKeyIsNumber) 
  24.         { 
  25.           $key = array_slice($row, $indexKey, 1); 
  26.           $key = (is_array($key) && ! emptyempty($key)) ? current($key) : NULL; 
  27.           $key = is_null($key) ? 0 : $key; 
  28.         } 
  29.         else 
  30.         { 
  31.           $key = isset($row[$indexKey]) ? $row[$indexKey] : 0; 
  32.         }//www.Vevb.com 
  33.       } 
  34.  
  35.       $result[$key] = $tmp; 
  36.     } 
  37.  
  38.     return $result; 
  39.   } 
  40. } 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表