-
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.
-
array_combine
Posted on July 23rd, 2009 3 commentsThe array_combine() function creates an associative array (hash) by using one array for keys and another for values.
PHP
$a = array('python', 'lisp', 'perl'); $b = array('PY', 'LI', 'PE'); $c = array_combine($a, $b); print_r($c); => /* Array ( [python] => PY [lisp] => LI [perl] => PE ) */
To replicate this functionality in Ruby, we need to use a Hash object, since arrays in Ruby don’t use associative key/value pairs.
Since there is no exact equivalent of to the array_combine() function in Ruby, we manually create a hash from two different arrays.
Ruby
p1 = ['python', 'lisp', 'perl']; p2 = ['PY', 'LI', 'PE']; # initialize the hash combined_hash = {} # build the hash from 2 different arrays p2.each_with_index do |val, key| combined_hash[p1[key]] = val end # print resulting hash p combined_hash => # ["python"=>"PY", "lisp"=>"LI", "perl"=>"PE"]


