What 20 Lines (or less) of code did you find really useful?

Posted by Ygam on Stack Overflow See other posts from Stack Overflow or by Ygam
Published on 2010-03-13T07:32:02Z Indexed on 2010/03/13 7:35 UTC
Read the original article Hit count: 151

You can share your code or other's code. Here's a snippet from an array function in Kohana:

public static function rotate($source_array, $keep_keys = TRUE)
    {
        $new_array = array();
        foreach ($source_array as $key => $value)
        {
            $value = ($keep_keys === TRUE) ? $value : array_values($value);
            foreach ($value as $k => $v)
            {
                $new_array[$k][$key] = $v;
            }
        }

        return $new_array;
    }

It was helpful when I was uploading multiple images using multiple file upload forms. It turned this array

array('images' => array(
 'name' => array(
   0 => 'img1',
   1 => 'img0',
   2 =>'img2'
),
'error' => array(
 0 => '',
 1 => '',
 2 => ''

into :

array('images' => array(
 0 => array(
   'name' => 'img1'
    'error' => ''
),//rest goes here

How about you? What 20 or less lines of code did you find useful?

© Stack Overflow or respective owner

Related posts about language-agnostic

Related posts about programming