1 / 27

PHP Arrays Objects

PHP Arrays Objects. I. What is Arrays?. An array stores multiple values in one single variable. A variable is a storage area holding a number or text. The problem is, a variable will hold only one value. An array is a special variable, which can store multiple values in one single variable.

vangie
Download Presentation

PHP Arrays Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Arrays Objects

  2. I. What is Arrays? • An array stores multiple values in one single variable. • A variable is a storage area holding a number or text. The problem is, a variable will hold only one value. • An array is a special variable, which can store multiple values in one single variable. • Ex. $employee_array[0] = "Bob"; $employee_array[1] = "Sally"; $employee_array[2] = "Charlie"; $employee_array[3] = "Clare";

  3. An array can hold all your variable values under a single name. And you can access the values by referring to the array name. • Each element in the array has its own index so that it can be easily accessed. • In PHP, there are three kind of arrays: • Numeric array - An array with a numeric index • Associative array - An array where each ID key is associated with a value • Multidimensional array - An array containing one or more arrays

  4. 1. Numeric Arrays $names[key] = value; or $names = array("value0","value1","value2"); A numeric array stores each array element with a numeric index. Syntax : There are two methods to create a numeric array. 1. In the following example the index are automatically assigned (the index starts at 0): $fruits = array(“apple",”banana",”grape",”pinaple”);

  5. <html> <body> <?php $fruits = array(“apple",”banana",”grape",”pinaple”); foreach( $fruits as $value ) { echo "array of fruits: <b>$value</b> <br />"; } ?> </body> </html> Result :

  6. 2. In the following example we assign the index manually: $ fruits[0]=" Apple ";$ fruits[1]=" Banana ";$ fruits[2]=" Grape ";$ fruits[3]=" Orange"; Ex <?php $ fruits[0]=" Apple "; $ fruits[1]=" Banana "; $ fruits[2]=" Grape "; $ fruits[3]=" Orange"; foreach( $fruits as $value ) { echo "array of fruits: <b>$value</b> <br />"; } ?>

  7. Finding the Output : <html> <body> <hr>Array index</hr> <?php $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0] . " and " . $cars[3] . " are Swedish cars.<br/>"; echo $cars[1] . " and " . $cars[2] . " are Jepan cars.<br/>"; echo $cars[0] . $cars[1]. $cars[2] ." and " . $cars[3] . “ are not Korean cars.<br/>"; ?> </body> </html>

  8. Accessing Array Contents To access the contents of a variable, you use its name. If the variable is an array, you access the contents using the variable name and an index. The index indicates which of the values in the array you access. The index is placed in square brackets after the name. Ex: $numbers = array("one","two","three","four","five"); echo $numbers[3]; // four

  9. Using Loops to Access the Array Contents Because the array is indexed by a sequence of numbers, you can use a for loop to more easily display its contents: Ex . $numbers = array("one","two","three","four","five"); for($i=0;$i<5;$i++) { echo $numbers[$i]; }

  10. 2. Associative array $array_name=("key0"=>"value0","key1"=>"value1",…); or $array_name["key"]= "value"; An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them. Syntax : Set string value into array: Ex: $name[“firstname”] = “Rath”; $name[“lastname”] = “Rathana”; or $name = array(“firstname”=>”Rath”, “lastname”=>”Rathana”);

  11. The ID keys can be used in a script: <?php $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is " . $ages['Peter'] . " years old.";?> The code above will output: Peter is 32 years old. Note:If you wanted to simply store a list and not worry about the particular order, or what each value should be mapped to (such as a list of states or flavors of shaved ice), you don’t need to explicitly name the keys and PHP will assign invisible internal keys for processing.

  12. This would be set up as follows: <?php $flavor[] = "blue raspberry"; $flavor[] = "root beer"; $flavor[] = "pineapple"; ?> Using Loops to Access the Array Contents Because the indices in an array are not numbers, you cannot use a simple counter in a for loop to work with the array. However, you can use the foreach loop or the list( ) and each( ) constructs. 1. The foreach loop The foreach loop has a slightly different structure when using associative arrays. You can use it exactly as you did in the previous example, or you can incorporate the keys as well:

  13. Ex. <html> <body> <?php $salaries = array("sophal" => "500 $","srey" => "300 $", "narry" => "100 $"); foreach ($salaries as $key => $value) echo $key.” has salary: “.$value.”<br />”; ?> </body> </html> Out put : sophal has salary: 500$ srey has salary: 300$ narry has salary: 100$

  14. 2. each( ) construct The following code lists the contents of the $salaries array using the each() construct: <?php $salaries = array("sophal" => "500 $","srey" => "300 $", "narry" => "100 $"); while( $element = each( $salaries ) ) { echo $element[ 'key' ]; echo ' - '; echo $element[ 'value' ]; echo '<br />'; } ?>

  15. 3. list( ) construct There is a more elegant and more common way of doing the same thing. The construct list( ) can be used to split an array into a number of values. You can separate two of the values that the each( ) function gives you like this: $salaries = array("sophal" => "500 $","sreyleak" => "300 $", "narry" => "100 $"); list( $employee, $value ) = each( $salaries); This line uses each() to take the current element from $prices, return it as an array, and make the next element current. It also uses list() to turn the 0 and 1 elements from the array returned by each() into two new variables called $employee and $value.

  16. You can loop through the entire $value array, echoing the contents using this short script: $salaries = array("sophal" => "500 $","sreyleak" => "300$“,"narry" => "100 $");  while( list( $employee, $value )= each($salaries)) { echo “$employee - $value <br />”; }

  17. 3. Multidimensional Arrays In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Ex .

  18. Result :

  19. III. PHP Array Functions PHP: indicates the earliest version of PHP that supports the function. Paper..

  20. IV. PHP Array Constants PHP: indicates the earliest version of PHP that supports the constant.

  21. V. PHP Array Sort • Sort can be use to arrange the character (from A to Z or from Z to A) or Number(from big to small or small to big numbers) Ascenting and Descening . • In Array have 4 sort are : • ksort() Ascenting Characters • krsort () Descenting Characters • asort() Ascenting Numbers • arsort() Descenting Numbers

  22. 1. Ksort () Ascenting To sort Characters from A to Z. Ex <?php $book = array("Java"=>"10", "Database"=>"30","Network"=>"2", "Web"=>"15"); ksort($book); echo"ksort book Ascenting<br>"; foreach ($book as $sheet => $book) { echo $sheet." amount "; echo $book ."<br>"; } ?>

  23. 2. krsort () Descenting To sort Characters from Z to A . Ex <?php $book = array("Java"=>"10", "Database"=>"30","Network"=>"2", "Web"=>"15"); krsort($book); echo"krsort book Ascenting<br>"; foreach ($book as $sheet => $book) { echo $sheet." amount "; echo $book ."<br>"; } ?>

  24. 3. asort() Ascenting Numbers To sort Number from smaller to bigger numbers (1 to ). Ex <?php $book = array("Java"=>"10", "Database"=>"30","Network"=>"2", "Web"=>"15"); asort($book); echo"asort book Ascenting<br>"; foreach ($book as $sheet => $book) { echo $sheet." amount "; echo $book ."<br>"; } ?>

  25. 4. arsort() Descenting Numbers To sort Number from bigger to smaller numbers ( to 1). Ex <?php $book = array("Java"=>"10", "Database"=>"30","Network"=>"2", "Web"=>"15"); arsort($book); echo"arsort book Ascenting<br>"; foreach ($book as $sheet => $book) { echo $sheet." amount "; echo $book ."<br>"; } ?>

  26. Homework :

  27. THE END

More Related