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

2015-01-24信息快讯网

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

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

if( ! function_exists('array_column'))
{
  function array_column($input, $columnKey, $indexKey = NULL)
  {
    $columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE;
    $indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE;
    $indexKeyIsNumber = (is_numeric($indexKey)) ? TRUE : FALSE;
    $result = array();

    foreach ((array)$input AS $key => $row)
    { 
      if ($columnKeyIsNumber)
      {
        $tmp = array_slice($row, $columnKey, 1);
        $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : NULL;
      }
      else
      {
        $tmp = isset($row[$columnKey]) ? $row[$columnKey] : NULL;
      }
      if ( ! $indexKeyIsNull)
      {
        if ($indexKeyIsNumber)
        {
          $key = array_slice($row, $indexKey, 1);
          $key = (is_array($key) && ! empty($key)) ? current($key) : NULL;
          $key = is_null($key) ? 0 : $key;
        }
        else
        {
          $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
        }
      }

      $result[$key] = $tmp;
    }

    return $result;
  }
}
php使用正则表达式获取图片url的方法
php使用CURL伪造IP和来源实例详解
php+mysql实现无限分类实例详解
php截取html字符串及自动补全html标签的方法
php在linux下检测mysql同步状态的方法
腾讯微博提示missing parameter errorcode 102 错误的解决方法
php运行提示:Fatal error Allowed memory size内存不足的解决方法
php提示Warning:mysql_fetch_array() expects的解决方法
PHP使用array_multisort对多个数组或多维数组进行排序
php中Array2xml类实现数组转化成XML实例
PHP中array_slice函数用法实例详解
php中in_array函数用法探究
PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别
PHP利用func_get_args和func_num_args函数实现函数重载实例
php下foreach提示Warning:Invalid argument supplied for foreach()的解决方法
Windows下安装PHP单元测试环境PHPUnit图文教程
PHP中soap的用法实例
php中socket的用法详解
PHP利用header跳转失效的解决方法
PHP与MYSQL中UTF8 中文排序示例代码
php cookie名使用点号(句号)会被转换
PHP中echo,print_r与var_dump区别分析
安装ImageMagick出现error while loading shared libraries的解决方法
php中strstr、strrchr、substr、stristr四个函数的区别总结
重新认识php array_merge函数
PHP函数in_array()使用详解
PHP中array_map与array_column之间的关系分析
php中使用array_filter()函数过滤空数组的实现代码
PHP JSON出错:Cannot use object of type stdClass as array解决方法
VB中的RasEnumConnections函数返回632错误解决方法
PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
PhpDocumentor 2安装以及生成API文档的方法
©2014-2024 dbsqp.com