找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1911|回复: 0
打印 上一主题 下一主题

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

[复制链接]

2647

主题

2647

帖子

7881

积分

论坛元老

Rank: 8Rank: 8

积分
7881
跳转到指定楼层
楼主
发表于 2018-2-14 05:53:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            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 array_flip() 删除数组重复元素
  • PHP合并数组+与array_merge的区别分析
  • php数组函数序列之array_key_exists() - 查找数组键名是否存在
  • php数组函数序列之array_unique() - 去除数组中重复的元素值
  • php数组函数序列之array_keys() - 获取数组键名
  • php array_filter除去数组中的空字符元素
  • php5.5新数组函数array_column使用
  • PHP中unset,array_splice删除数组中元素的区别
  • PHP中array_map与array_column之间的关系分析
  • php提示Warning:mysql_fetch_array() expects的解决方法
  • php中array_column函数简单实现方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端