1   <?php
  2   /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3   // +----------------------------------------------------------------------+
  4   // | PHP Version 4                                                        |
  5   // +----------------------------------------------------------------------+
  6   // | Copyright (c) 1997-2004 The PHP Group                                |
  7   // +----------------------------------------------------------------------+
  8   // | This source file is subject to version 3.0 of the PHP license,       |
  9   // | that is bundled with this package in the file LICENSE, and is        |
  10   // | available at through the world-wide-web at                           |
  11   // | http://www.php.net/license/3_0.txt.                                  |
  12   // | If you did not receive a copy of the PHP license and are unable to   |
  13   // | obtain it through the world-wide-web, please send a note to          |
  14   // | license@php.net so we can mail you a copy immediately.               |
  15   // +----------------------------------------------------------------------+
  16   // | Authors: Aidan Lister <aidan@php.net>                                |
  17   // +----------------------------------------------------------------------+
  18   //
  19   // $Id: array_chunk.php,v 1.1 2004-10-26 18:22:16 kostja Exp $
  20   //
  21  
  22  
  23   /**
  24    * Replace array_combine()
  25    *
  26    * @category    PHP
  27    * @package     PHP_Compat
  28    * @link        http://php.net/function.array_chunk
  29    * @author      Aidan Lister <aidan@php.net>
  30    * @author      Thiemo M�ttig (http://maettig.com)
  31    * @version     $Revision: 1.1 $
  32    * @since       PHP 4.2.0
  33    * @require     PHP 4.0.1 (trigger_error)
  34    */
  35   if (!function_exists('array_chunk'))
  36   {
  37       function array_chunk ($input, $size, $preserve_keys = false)
  38       {
  39           if (!is_array($input)) {
  40               trigger_error('array_chunk() expects parameter 1 to be array, ' . gettype($input) . ' given', E_USER_WARNING);
  41               return null;
  42           }
  43          
  44           if (!is_numeric($size)) {
  45               trigger_error('array_chunk() expects parameter 2 to be long, ' . gettype($size) . ' given', E_USER_WARNING);
  46               return null;
  47           }
  48          
  49           $size = (int)$size;
  50           if ($size <= 0)
  51           {
  52               trigger_error('array_chunk() Size parameter expected to be greater than 0', E_USER_WARNING);
  53               return null;
  54           }
  55  
  56           $chunks = array();
  57           $i = 0;
  58  
  59           if ($preserve_keys !== false)
  60           {
  61               foreach ($input as $key => $value) {
  62                   $chunks[(int)($i++ / $size)][$key] = $value;
  63               }
  64           }
  65           else
  66           {
  67               foreach ($input as $value) {
  68                   $chunks[(int)($i++ / $size)][] = $value;
  69               }
  70           }
  71  
  72           return $chunks;
  73       }
  74   }
  75  
  76   ?>