LOOPS and Arrays Notes by malj

VIEWS: 3 PAGES: 8

									Leever – Php                                                            1


                   LOOPS and Arrays Notes


WHILE LOOP

while(this is true)                  while($i < 10)
{                                    {
Do this                              print "$i";
}                                    $i++;
                                     }



DO   WHILE LOOP


do {                                 do
All of this                          {
}                                    print $j;
while(this is true);                 $j++;
                                     }
                                     while($j < 4);


FOR LOOP


for(initial expression; text to      for($k=0; $k < 6; $k++)
run; modification of expression) {   {
Do all this                          print $k;
}
                                     }

BREAK STATEMENT: can be used in both a while and for loop. Allows you
to stop the loop completely under certain circumstances.

       for( expression; test; modification)
       {
             if (this is true)
             break;
             Do this;
       }

CONTINUE STATEMENT: Allows you to skip over an iteration (FOR LOOP
ONLY);

       for(expression; test; modification)
       {
       if (this is true)
       continue;
       Do this
       }
Leever – Php                                                             2

FOREACH LOOPS: For use with ARRAYS


Foreach LOOP

         foreach($array as $temp) {
         do this
         }


               <?php
               $animals = array("Cat", "Dog", "Horse", "Bird", "Pig");

               foreach($animals as $temp)
               {
               print "<li>$temp</li>";
               }
               ?>

While LOOP
Used with list() and each():

         list($key, $val)

         each() which returns the current key and value pair from an
array.

               <?php
               $animals = array("Cat", "Dog", "Horse", "Bird", "Pig");

               while (list($key, $val) = each($animals))
               {
               print "$val <br />";
               }
               ?>




ARRAYS
Associative Arrays:

               $people = array (
                     "name" => "Carol",
                     "occupation" => "Teacher",
                     "favcolor" => "Blue",
                     "favanimal" => "Cat");

Can access anything in here by the key:

               print "$people[favanimal]    will return "Cat";


ADD TO THE     ARRAY
               <?php
               $people = array(
Leever – Php                                                 3

                     "name" => "Carol",
                     "occupation" => "Teacher",
                     "favcolor" => "Blue",
                     "favanimal" => "Cat");

               $people['favfood'] = "Chocolate";

               print $people['favfood'];
               ?>


Loop Through an Associative Array: -- using FOREACH

               <?php
               $people = array(
                     "name" => "Carol",
                     "occupation" => "Teacher",
                     "favcolor" => "Blue",
                     "favanimal" => "Cat");

               foreach ($people as $key=>$val)
               {
               print "$key = $val <br />";
               }
               ?>


Looping Through an Associative Array --- using WHILE

               <?php
               $people = array(
                       "name" => "Carol",
                       "occupation" => "Teacher",
                       "favcolor" => "Blue",
                       "favanimal" => "Cat");

               while (list ($key, $val) = each ($people) )
               {
               print "$key ---- $val <br />";
               }
               ?>


Multidimensional Arrays: with Loop

               <?php
               $people = array(

                     array(
                     "name" => "Carol",
                     "occupation" => "Teacher",
                     "favcolor" => "Blue",
                     "favanimal" => "Cat"),

                     array (
                     "name" => "Matt",
                     "occupation" => "Doctor",
Leever – Php                                             4

                     "favcolor" => "Green",
                     "favanimal" => "Dog"),

                     array (
                     "name" => "Terry",
                     "occupation" => "Broker",
                     "favcolor" => "Red",
                     "favanimal" => "Dog")

               );

               foreach ($people as $temp)
               {
                     foreach ($temp as $key=>$val)
                     {
                     print "$key = $val<br />";
                     }
               print "<br />";
               }
               ?>


Headers on Multidimensional Arrays:

               <?php
               $animals = array(
                       "Group One" => array(
                               "Feline" => "Tabby",
                               "Canine" => "Sheepdog",
                               "Avian" => "Sparrow"),

                       "Group Two" => array(
                               "Feline" => "Tiger",
                               "Canine" => "Poodle",
                               "Avian" => "Eagle",
                               "Bovine" => "Cow"),

                       "Group Three" => array(
                               "Feline" => "Siamese",
                               "Canine" => "Terrier",
                               "Avian" => "Hawk")
                       );

               foreach($animals as $key=>$temp)
               {
               print "$key<br />";
                     foreach ($temp as $key=>$val)
                     {
                     print "$key -- $val<br />";
                     }
               print "<hr />";
               }
               ?>
Leever – Php                                                          5

JOINING ARRAYS: Creates a NEW array but leaves the original arrays
alone.
            <?php
            $first = array("Cat", "Dog", "Bird");
            $second = array("Meow", "Woof", "Chirp");
            $third = array_merge($first, $second);

               foreach ($third as $temp)
               {
               print "$temp <br />";
               }
               ?>


ARRAY PUSH— Pushing changes the total value of the first array:
            <?php

               $first = array("Cat", "Dog", "Bird");
               array_push($first, "Meow", "Woof", "Chirp");

               foreach ($first as $temp)
               {
               print "$temp <br />";
               }
               ?>


ARRAY_UNSHIFT: does what the PUSH does but adds to the front of the
array
            <?php
            $color = array("red", "Blue", "green");

               array_unshift($color, "Orange", "Purple", "Silver");
               foreach ($color as $temp)
               {
               print "$temp<br />";
               }
               ?>


SORTING ARRAYS

sort($arrayname) – for normal order
rsort($arrayaname) – for reverse order

SORTING ASSOCIATIVE ARRAYS:
asort($arrayname); will sort the VALUES and preserve the keys
arsort($arrayname) – for reverse order
ksort($arrayname) – will sort the keys;
krsort($arrayname) – keys in reverse order;



ARRAY_UNIQUE: Removes any duplicate values in the array.

$color = array_unique($color);
Leever – Php                                                          6



Searching an Array:

This will return the number 1 if it is present:
      in_array("thing to look for", $array);


      <?php
      $animals = array("cat", "Dog", "horse");
      print in_array("Dog", $animals);
      ?>

      OR
      <?php
      $confirm = in_array("Dog", $animals);
      print "$confirm";
      ?>


This will return the key of the item in the array:

      array_search("thing to look for", $array);

      <?php
      $animals = array("cat", "Dog", "horse");
      print array_search("horse", $animals);
      ?>

               OR


      <?php
      $confirm = array_search("horse", $animals);
      print "$confirm";
      ?>

is_array() - This will return a 1 if true;

               if(is_array($var))
               {
               do this;
               }




Searching With a REGULAR EXPRESSION; (There is a
separate handout on Regular Expressions.)
      preg_match(pattern to look for, the thing to search through);
      ereg(pattern to look for, what to search, where to store it)

      Just as a Conditional:
                  <?php
                  $num = "5";
Leever – Php                                                               7

                    if(preg_match("/^\d+/", $num))
                    {
                    print "Matches for numbers";
                    }
                    else
                    {
                    print "Not numbers!";
                    }
                    ?>

               or

                    <?php
                    $word = "catastrophe";
                    if(ereg("cat", $word))
                    {
                    print "Matches for the word 'cat'";
                    }
                    else
                    {
                    print "Word 'cat' is not there!";
                    }
                    ?>


      USING IN LOOPS:



      This first foreach loop will find "Horse.jpg" and Bird.jpg" and
      print that 'print' line twice.

                    <?php
                    $animals = array("Cat", "Dog", "Rat", "Horse.jpg",
                    "Bird.jpg", "Pig");

                          foreach($animals as $temp)
                          {
                                if(preg_match("/^\w+.jpg/", $temp))
                                {
                                  print "Found Jpg File -- $temp<br />";
                                }
                          }
                    ?>

      This second foreach loop will find the '5' in 35 and the '5' all
      by itself and print the print line 2 times.

                    <?php
                    $numbers = array(23,35,4,5,6);

                          foreach($numbers as $temp)
                          {
                                    if(ereg("5", $temp))
                                    {
                                    print "Found the number 5<br />";
                                    }
Leever – Php                                                             8

                                }

                      ?>



PATTERN MATCHING AND REPLACING

preg_preplace("pattern to look for", "how to replace", "what to
search");


               <?php
               $animals = array("dog.jpg", "cat.jpg", "cow", "mouse");

               $pattern = "/jpg/";

               $replace = "gif";

               foreach ($animals as $temp)
               {
               print preg_replace($pattern, $replace, $temp);
               print "<br>";
               }
               ?>



split();   -- splits a string
               $wordlist = "cat, dog, horse, bird";
               $wordarray = split(", ", $wordlist);
               foreach($wordarray as $temp)
               {
               print "$temp<br />";
               }


preg_split(); Splits with a regular expression


               $wordlist = "cat, dog, horse, bird";
               $wordarray = preg_split("/,/", $wordlist);
               foreach($wordarray as $temp)
               {
               print "$temp<br />";
               }

								
To top