Arrays Tool

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Arrays Tool

    Here is a simple noob class that helps the dev with working with arrays. It's quite similar to C# LINQ (very little).
    Arr.class.php
    PHP Code:
    class Arr
    {
        public static 
    $instance null;
        
        private 
    $array;
        private 
    $prep_array;
        
        public function 
    __construct(array $array)
        {
            
    $this->array $array;
        }
        
        public function 
    __destruct()
        {
            
    $this->array NULL;
            
    $this->prep_array NULL;
        }
            
        public static function 
    init(array $array)
        {
            if (
    self::$instance == NULL)
            {
                
    $class_name __CLASS__;
                
    self::$instance = new $class_name($array);
            }
            return 
    self::$instance;
        }
        
        public function 
    select($code)
        {       
            
    $keys explode(','$code);        
            if (
    strtolower($code) != 'all' && is_array($keys))
            { 
                foreach (
    $this->array AS $key => $value)
                    if (
    key_exists($key$keys))
                        
    $this->prep_array[$key] = $value;
            } 
            else
            {
                if (
    strtolower($code) == 'all')
                    
    $this->prep_array $this->array;
                else
                {
                    if (isset(
    $this->array[$code]))
                        
    $this->prep_array[$code] = $this->array[$code];
                }
            }
            
            return 
    $this;
        } 
        
        public function 
    limit()
        {
            
    $arr func_get_args();
            
            if (
    count($arr) == 2)
            {
                
    $limit_start $arr[0];
                
    $limit_end $limit_start $arr[1];                            
            }
            else
            {
                
    $limit_start 0;
                
    $limit_end $limit_start $arr[0];
            }
            
            
    $i $found 0;
            foreach (
    $this->prep_array AS $key => $value)
            {
                if (
    $limit_start $i || $limit_end <= $i)
                {
                    unset(
    $this->prep_array[$key]);                
                }
                
    $i++;
            }                
            return 
    $this;
        }
        
        public function 
    orderASC()
        {
            
    asort($this->prep_array);
            
            return 
    $this;
        }
        
        public function 
    reverse()
        {          
            
    $this->prep_array array_reverse($this->prep_array);
            
    ksort($this->prep_array);
        
            return 
    $this;
        }
        
        public function 
    orderDESC()
        {
            
    arsort($this->prep_array);
            
            return 
    $this;
        }    
        
        public function 
    exec($type 'return')
        {
            switch (
    strtolower($type))
            {
                case 
    'return' : return $this->prep_array; break;
                case 
    'print_r':
                { 
                    echo 
    '<pre>'
                    
    print_r($this->prep_array); 
                    echo 
    '</pre>';
                } 
                break;            
                case 
    'dump' var_dump($this->prep_array); break;
                default: return 
    $this->prep_array; break;             
            }
            return 
    $this->prep_array;
        }
        
        public function 
    reset()
        {
            
    $this->prep_array $this->array;
            
            return 
    $this;
        }
        
        public function 
    as_object()
        {
            
    $this->prep_array = (object) $this->prep_array;
            
            return 
    $this;
        }

        public function 
    as_array()
        {
            
    $this->prep_array = (array) $this->prep_array;
            
            return 
    $this;
        }
        
        public function 
    between($first$last)
        {
            
    $found_first false;
            
    $found_last false;
            
            foreach (
    $this->prep_array AS $key => $value)
            {
                if (
    $found_first == false)
                {
                    if (
    $key <= $first)
                        unset (
    $this->prep_array[$key]);
                    else
                    {
                        
    $found_first true;
                        continue;
                    }
                }
                
                if (
    $found_last == false && $found_first == true)
                {
                    if (
    $key >= $last)
                        unset (
    $this->prep_array[$key]);
                }
            }
            
            return 
    $this;
        }
          
        public static function 
    get($array$key$default NULL)
        {
            return isset(
    $array[$key]) ? $array[$key] : $default;
        }
        
        public static function 
    extract($array, array $keys$default NULL)
        {
            
    $found = array();
            foreach (
    $keys as $key)
            {
                
    $found[$key] = isset($array[$key]) ? $array[$key] : $default;
            }

            return 
    $found;
        }

        public static function 
    range($step 10$max 100)
        {
            if (
    $step 1)
                return array();

            
    $array = array();
            for (
    $i $step$i <= $max$i += $step)
            {
                
    $array[$i] = $i;
            }

            return 
    $array;
        }
        
        public static function 
    null_array($number 0)
        {
            if (
    $number 0)
            {
                
    $array = array();
                for (
    $i 0$i $number$i++)
                {
                    
    $array[$i] = null;
                }
                return 
    $array;
            }
            return array();
        }
        
        public static function 
    is_multi(array $array)
        {
            return 
    count($array) == count($arrayCOUNT_RECURSIVE);         
        }        

    Samples:

    PHP Code:
    require('Arr.class.php');

    //ex1
    $arr3 = array('a' => 1'b'=> 3'c'=>3'd'=>4'e'=>5'f'=>7);
    Arr::init($arr3)->select('ALL')->between('b''d')->exec('dump');

    $arr = array(12345678910111213141516);

    Arr::init($arr)->select('1,2,3,4,5,6,7,8,9')->orderDESC->exec('print_r');

    var_dump(Arr::init($arr)->between(36)->exec());

    Arr::init($arr)->between(36)->limit(05)->exec('var_dump');

    Arr::init($arr)->as_object()->exec('var_dump');

    //Limit(start, number_of_elements);

    //Arr::get(ARRAY, KEY, DEFAULT = OPTIONAL); // Get a specific key from an array

    //Arr::extract(ARRAY, array of keys, DEFAULT); 

    //Arr::null_array(SIZE); - Make a null array with size N

    //Arr->reset(); - Reset the current array

    //Arr::range(STEP, MAX); - Make an array with steps ex: range(10, 100) -> 10, 20, 30, 40 ... 100

    //Arr::is_multi(ARRAY); check if an array is multidimensional 
    You can combine the methods (or class functions) whatever you like but is strictly necessary to end with ->exec();
    I wait for more ideas to improve this "extension":D
    Last edited by i0nutzxp; 29.01.12, 16:29.
    <?php unlink('World/Europe/Romania.country'); ?>
Working...
X