Workshop in Computational Bioskills - Lesson 11 Arrays

o The definition of Arrays in PHP includes both lists (associative
  arrays) and hashes, where the list is a specific case of an hash.
o The name of the hash/list is a regular variable name - beware
  not to override existing variables. 
o The array keys must be scalar (string or number), and the
  array elements can be either scalars or arrays.
Associative Array (Hash) Declaration:

By calling the constructor: 

$vegetables = array( 'corn' => 'yellow', 
                    'carrot' => 'orange',
                    'tomato' => 'red' );

The keys can be numbers, string or both. 

Or by a direct assignment: 

$vegetables['corn'] = 'yellow';
$vegetables['carrot'] = 'orange';
$vegetables['tomato'] = 'red';

$dinner[0] = 'chicken';
$dinner[1] = 'rice';

$price['perl book'] = 29.99;
$price['php book'] = 25.0;

List Declaration:

When the keys are ordered integers, the hash is very much like an
ordered list. PHP has a shortcut to generate a list: 


$dinner = array('chicken', 'rice');
print "$dinner[1]"; // rice




Array Operators

o An array element can be manipulated like a regular variable: 


$dinner[1] = 'potato'; // assignment

$cost = $price['perl book'] + $price['php book']; //arithmetic operations

unset( $price['perl book'] ); // removes the element from the array.
                              // It is different from 
			      // setting the value to zero or to empty string 


o $arr[] - add a new element to the end of array: 

         $dinner[] = 'ice-cream'; // $dinner[2] is now 'ice-cream'.


o count( $arr ) - return the number of elements in the array.


o foreach($arr as $key=>$value) - iterate over the hash
elements: 

foreach ($vegetables as $veg=>$color)
{
// the values are copied to $veg and $color
 // therefore changing their values does not affect the list.
 print "<tr><td>$veg</td><td>$color</td></tr>\n";
}


o foreach($arr as $value) - shortcut for lists:  

foreach ($dinner as $dish)
{
 print "going to eat $dish\n";
}


Note: The element are accessed in the order they were added to the 
array. if you want to access them with index order, use for() loop. 

o Finding an element in the array: 

// array_key_exists looks for the given key in the array and 
// returns true or false; 

if (array_key_exists('corn',$vegetables))
{ 
 print "yes, we have corn\n"; 
}


// in_array looks for the given value in the array and 
// returns true or false. The search is case sensitive:

if (in_array('red',$vegetables)
{
 print "yes, we found something red in the vegs list\n"; 
}


// array_search looks for the given value in the array and 
// returns its key.

$key = array_search('red',$vegetables);
if ($key)
{
 print "the red vegetable is: $key\n";
}


o implode($delim, $arr) -
print of the value list (like perl join).

print "".implode("",$dinner)."";

o explode($string) - split string into an array.

$colors = 'red, white, blue, green';

$color_list = explode(', ',$colors);



Array Sorting

Lexicographic ordering of the array elements:
  • sort() - sorts lists values (increasing) Remove the keys therefore it is not good for sorting hashes
  • asort() - sorts hashes according to their values. Keeps the associated keys.
  • ksort() - sorts hashes according to their keys.
  • rsort(), arsort(), krsort() do the same but with decreasing order of the elements


Multidimensional Arrays
The value of an element can be itself an array. 

// Declaration

$prices =  array(
           'red' => array('apple'=> 7.00, 'tomato'=> 4.30 ),
	   'green' => array('letus' => 2.00,'cucumber' => 3.5),
	   'yellow' => array('corn' => 10.00 ,'lemon' => 5.05 ) );

// Now let's print it:

print $prices['red']['tomato']; // 4.30
print $prices['green']['cucumber']; // 3.5