Location: PHPKode > projects > Anahita Social Engine > dependencies/plg_system_socialengine/socialengine/util/array/helper.php
<?php
/**
 * @version		1.0.0
 * @category	Anahita Social Engine™
 * @copyright	Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
 * @license		GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 * @link     	http://www.anahitapolis.com
 */

class AnUtilArrayHelper extends KHelperArray
{
	/**
	 * index an object (or an array) using one of it's attribute
	 * @return 
	 * @param $items Array
	 * @param $attr  String
	 */
	public static function indexBy( $items, $attr)
	{
		$newArray = array();
		foreach($items as $item) {			
			$newArray[is_array($item) ? $item[$attr] : $item->$attr] = $item;
		}
		return $newArray;
	}	
	
	/**
	 * flats an array
	 * @return 
	 * @param $array Object
	 */
	static public function flatten($array)
	{	
	    if (!is_array($array) || empty($array)) return '';
	    if (empty($flat)) $flat = array();
	       
	    foreach ($array as $key => $val) {
	      if (is_array($val)) $flat = self::flatten($val, $flat);
	      else $flat[] = $val;
	    }
	       
	    return $flat; 	
	}
	
	/**
	 * collects the values of $attrs of every item within a list
	 * @return 
	 * @param $items Object
	 * @param $attrs Object
	 * @param $unique Object[optional]
	 */
	static public function collect($items, $attrs, $unique=false)
	{
		$collection = array();
		
		settype($attrs, 'array');
			
		foreach($items as $item) {
			foreach($attrs as $attr)
				$collection[] = is_array($item) ? $item[$attr] : $item->$attr;
		}
		
		if ($unique) $collection = array_unique($collection);
			
		return $collection;
	}
	
	/**
	 * Set an array of key/value attributes for a list of items
	 * @param $items Array
	 * @param $attrs Array
	 */
	static public function set($items, $attrs)
	{
		foreach($items as $item) { 
			foreach($attrs as $key => $value) {
				if ( is_array($item) ) 
					$item[$key] = $value;
				else
					$item->$key = $value;
			}
		}
	}
	
	/**
	 * converts an array to string.
	 * @return 
	 * @param $array Array[optional]
	 * @param $inner_glue String[optional]
	 * @param $outer_glue String[optional]
	 * @param $keepOuterKey Boolean[optional]
	 * @param $quote Boolean[optional]
	 */
	public static function toString( array $array = null, $inner_glue = '=', $outer_glue = ' ', $keepOuterKey = false, $quote = true )
	{
		$output = array();

		if (is_array($array))
		{
			foreach ($array as $key => $item)
			{
				if (is_array ($item))
				{
					if ($keepOuterKey) {
						$output[] = $key;
					}

					// This is value is an array, go and do it again!
					$output[] = KHelperArray::toString( $item, $inner_glue, $outer_glue, $keepOuterKey);
				}
				else { 
					$output[] = $key.$inner_glue.($quote ? '"'.$item.'"' : $item);
				}
			}
		}

		return implode( $outer_glue, $output);
	}
	
	
	/**
	 * Check if the array is an associative array
	 * @return BOOL
	 * @param $array Object
	 */
	public static function isAssociative(array $array )
	{	
	   if ( is_array($array) && ! empty($array) )
	    {
	        for ( $iterator = count($array) - 1; $iterator; $iterator-- )
	        {
	            if ( ! array_key_exists($iterator, $array) ) { return true; }
	        }
	        return ! array_key_exists(0, $array);
	    }
	    return false;	
	}
}
Return current item: Anahita Social Engine