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


Posted in PHP onOctober 24, 2014

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 相关文章推荐
mysql 全文搜索 技巧
Apr 27 PHP
php中强制下载文件的代码(解决了IE下中文文件名乱码问题)
May 09 PHP
php使用mkdir创建多级目录入门例子
May 10 PHP
PHP+jquery+ajax实现即时聊天功能实例
Dec 23 PHP
php定义参数数量可变的函数用法实例
Mar 16 PHP
php简单计算页面加载时间的方法
Jun 19 PHP
WordPress中调试缩略图的相关PHP函数使用解析
Jan 07 PHP
YII Framework框架教程之日志用法详解
Mar 14 PHP
简单谈谈PHP中的include、include_once、require以及require_once语句
Apr 23 PHP
php微信支付接口开发程序
Aug 02 PHP
php+resumablejs实现的分块上传 断点续传功能示例
Apr 18 PHP
Thinkphp框架使用list_to_tree 实现无限级分类列出所有节点示例
Apr 04 PHP
PHP中soap的用法实例
Oct 24 #PHP
php中socket的用法详解
Oct 24 #PHP
PHP利用header跳转失效的解决方法
Oct 24 #PHP
PHP与MYSQL中UTF8 中文排序示例代码
Oct 23 #PHP
php cookie名使用点号(句号)会被转换
Oct 23 #PHP
php格式化时间戳显示友好的时间实现思路及代码
Oct 23 #PHP
PHP正则表达式 /i, /is, /s, /isU等介绍
Oct 23 #PHP
You might like
如何利用php array_multisort函数 对数据库结果进行复杂排序
2013/06/08 PHP
php版本CKEditor 4和CKFinder安装及配置方法图文教程
2019/06/05 PHP
破除一些网站复制、右键限制
2006/11/04 Javascript
JavaScript实现网页图片等比例缩放实现代码及调用方式
2013/02/25 Javascript
jQuery之字体大小的设置方法
2014/02/27 Javascript
JS实现的通用表单验证插件完整实例
2015/08/20 Javascript
Angular中$cacheFactory的作用和用法实例详解
2016/08/19 Javascript
vue中get请求如何传递数组参数的方法示例
2019/11/08 Javascript
在Heroku云平台上部署Python的Django框架的教程
2015/04/20 Python
python中关于for循环的碎碎念
2017/06/30 Python
Python中XlsxWriter模块简介与用法分析
2018/04/24 Python
TensorFlow数据输入的方法示例
2018/06/19 Python
CentOS6.9 Python环境配置(python2.7、pip、virtualenv)
2019/05/06 Python
python三大神器之fabric使用教程
2019/06/10 Python
Python生成验证码、计算具体日期是一年中的第几天实例代码详解
2019/10/16 Python
pandas实现将日期转换成timestamp
2019/12/07 Python
python读取Kafka实例
2019/12/23 Python
Python3列表List入门知识附实例
2020/02/09 Python
Python3实现飞机大战游戏
2020/04/24 Python
Pytorch高阶OP操作where,gather原理
2020/04/30 Python
欧洲最大的滑雪假期供应商之一:Sunweb Holidays
2018/01/06 全球购物
英国二手iPhone、音乐、电影和游戏商店:musicMagpie
2018/10/26 全球购物
allbeauty美国:英国在线美容店
2019/03/11 全球购物
Mansur Gavriel官网:纽约市的一个设计品牌
2019/05/02 全球购物
W Hamond官网:始于1979年的钻石专家
2020/07/20 全球购物
兼职业务员岗位职责
2014/01/01 职场文书
汽车专业大学生职业生涯规划范文
2014/01/07 职场文书
展会邀请函范文
2014/01/26 职场文书
音乐专业自荐信
2014/02/07 职场文书
观看《永远的雷锋》心得体会
2014/03/12 职场文书
诚信的演讲稿范文
2014/05/12 职场文书
2014年学生会部门工作总结
2014/11/07 职场文书
大学生,三分钟即兴演讲稿
2019/07/22 职场文书
vue实现锚点定位功能
2021/06/29 Vue.js
Java异常处理try catch的基本用法
2021/12/06 Java/Android