PHP to Ruby
Convert PHP code into Ruby!
-
array_chunk
Posted on October 17th, 2009 2 commentsThe 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"]]
-
Numeric Array
Posted on September 25th, 2009 No commentsNumeric Array
-
Multidimensional Array
Posted on September 24th, 2009 No commentsMultidimensional Array
-
Associative Array
Posted on September 22nd, 2009 No commentsAssociative Array
-
PHP Array
Posted on September 20th, 2009 No commentsA PHP array can be thought of as a variable which can store multiple other variables. Every item in a PHP array is known as an element and is composed of a key and a value. There are 2 main types of PHP arrays:
- Numeric Array
- Associative Array
Each of these can in turn be a Multidimensional Array.
Example Numeric Array:
$terms = array('array', 'element', 'key', 'value'); var_dump($terms); /* => Array ( [0] => array [1] => element [2] => key [3] => value ) */
Example Associative Array:
$terms = array( 'term1' => 'array', 'term2' => 'element', 'term3' => 'key', 'term4' => 'value' ); var_dump($terms); /* => Array ( [term1] => array [term2] => element [term3] => key [term4] => value ) */
Find out more information about PHP’s Numeric Array, Associative Array or Multidimensional Array.


