Friday, 8 July 2011

Arrays

$arr = array ("One","two","three");
Display
foreach($arr as $row)
  echo $row;
Modify
 foreach($arr as &$row)
   if($row=='one')
      $row="zero";

  unset($row);
The unset( $val ) line ensures that the $val variable is deleted after the loop has finished.
This is generally a good idea, because when the loop finishes, $val still holds a reference to the last
element. If you were to change $val later in your code, you would inadvertently
alter the last element of the $authors array. By unset ting (deleting) $val , you safeguard against this
potential bug.

Now, $arr will contain "zero" instead of "one".

Accessing both key and values of an array

  foreach($arr as $key=>$value)

$key will store the key and $value will store the value.

Multidimensional Arrays
eg:
$books = array(
array(
'title' => 'The Hobbit',
'authorId' => 2,
'pubYear' => 1937
),
array(
'title' => 'The Grapes of Wrath',
'authorId' => 0,
'pubYear' => 1939
),
array(
'title' => 'A Tale of Two Cities',
'authorId' => 3,
'pubYear' => 1859
),
);
Display
foreach($books as $rows)
 foreach($rows as $row)
   echo $row;

Manipulating arrays

Sorting
sort($array);  sorts in ascending order
rsort($array); descending
Sorting Associative arrays
Difference between indexed arrays and multi-dimensional arrays
eg for indexed array: $arr = array( "1"=>"indy","2"=>"john","3"=>"jaffer");
   associative array: $arr1 = array("title"=>"indy", "book"=>"DaVinci","bibli"=>"jugli");
if you give sort($arr1) for the associative array, it would output
           array("0"=>"DaVinci","1"=>"indy","2"=>"jugli");
Obviously, this is not what we want. We can use asort and arsort for this.
   eg: asort($arr1) will make $arr= array("book"=>"DaVinci","title"=>"indy","bibli"=>"jugli");
asort and arsort can be used for indexed arrays as well but they are usually not used.

ksort and krsor
  Sort arrays by their keys rather than the values.

multisort //Sort multiple arrays at the same time
eg:multisort($array1,$array2,$array3);
   sorts them one by one.

Adding and removing elements from the array
array_unshit() //Adds one or more new elements to the start of an array & displays the no. of elements in the upadted array

array_shift() //Removes first element and displays it 

array_push() //Adds an element to the end of the array

array_pop()  //deletes the last element

array_splice() //Removes element(s) from and/or adds element(s) to any point in an array

eg:$authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );
    echo array_unshift( $authors, “Hardy”, “Melville” ) . “ < br/ > ”; // Displays “6”
    // Displays “Array ( [0] = > Hardy [1] = > Melville [2] = > Steinbeck [3] = >
    Kafka [4] = > Tolkien [5] = > Dickens )”
    print_r( $authors );


array_splice()//
lets you remove a range of elements in an array and replace them with the elements
from another array. Both the removal and the replacement are optional, meaning you can just remove
elements without adding new ones, or just insert new elements without removing any. refer php.net

No comments:

Post a Comment