This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

array_chunk

The array_chunk() function in PHP splits an array into multiple smaller arrays (array chunks).

PHP

$langs = array('php', 'ruby', 'java', 'perl', 'csharp');
$my_size = 2;
var_dump( array_chunk($langs, $my_size) );

/*
Array (
    [0] => Array
        (
            [0] => php
            [1] => ruby
        )

    [1] => Array
        (
            [0] => java
            [1] => perl
        )

    [2] => Array
        (
            [0] => csharp
        )

) )
*/

Ruby

def array_chunk(full_array, size)
        number_of_chunks = (full_array.length/size.to_f).ceil;
        chunks = (1..number_of_chunks).collect { [] }
            while full_array.any?
              chunks.each do |a_chunk|
                a_chunk << full_array.shift if full_array.any?
              end
            end
        chunks
end

langs = ["php", "ruby", "java", "perl", "csharp"]
my_size = 2
p array_chunk(langs, my_size)

# => [["php", "perl"], ["ruby", "csharp"], ["java"]]
By | 2009-10-30T15:45:53+00:00 October 17th, 2009|Categories: PHP, Ruby|Tags: , , , |2 Comments

About the Author:

2 Comments

  1. david November 6, 2009 at 4:32 am

    errrmmm what about Array#each_slice, or Array#slice ??

    also, the method you implemented in ruby doesn’t have the same behaviour

    >> [“php”, “ruby”, “java”, “perl”, “csharp”].each_slice(2) do |chunk| puts chunk.inspect end
    [“php”, “ruby”]
    [“java”, “perl”]
    [“csharp”]

    ————————
    >> langs.slice!(0, 2)
    => [“php”, “ruby”]
    >> langs.slice!(0, 2)
    => [“java”, “perl”]
    >> langs.slice!(0, 2)
    => [“csharp”]

  2. tantalum August 6, 2010 at 8:56 am

    please replace this implementation,
    it is overcomplicated and inefficient,
    and there is “each_slice”.

Comments are closed.