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

yipeiwu_com6年前PHP代码库

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设计模式 注册表模式

下面是基本的注册表类的代码: 复制代码 代码如下: <?php class Registry { private static $instance; private $reques...

php上传大文件设置方法

打开php.ini,首先找到 ;;;;;;;;;;;;;;;; ; file uploads ; ;;;;;;;;;;;;;;;; 区域,有影响文件上传的以下几个参数: file...

PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的区别分析

本文分析了PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的区别。分享给大家供大家参考,具体如下: $_POST:通过 HTTP POST 方法传递的变...

php ci框架验证码实例分析

php代码:复制代码 代码如下:<?php class Captcha_code{ var $width='60'; var $num='4'; va...

php除数取整示例

示例: 复制代码 代码如下: $father = intval(floor($evalue/500))*500;...