1 / 14

PHP & MySQL

PHP & MySQL. PHP Arrays Objects. What is Array?. 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.

shalin
Download Presentation

PHP & MySQL

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 & MySQL

  2. PHP Arrays Objects • What is Array? • 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.

  3. PHP Arrays Objects (con.) • 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. PHP Arrays Objects (con.) EX: <?php $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>

  5. Example read array by call text <?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?>

  6. Example using array read array index by for loop <?php $cars=array("Volvo","BMW","Toyota"); $arrlength=count($cars); for($x=0;$x<$arrlength;$x++) { echo $cars[$x]; echo "<br>"; } ?>

  7. Example using array assign multi values to array and read by index <?php $main=array("A"=>array('x','y'), "B"=>array('u','v'), "C"=>array('w','z') ); echo $main['C'][0]; echo "<br>"; echo $main['C'][1]; ?>

  8. Example using array foreach loop <?php $colors = array("red","green","blue","yellow"); foreach ($colors as $value) { echo "$value <br>"; } ?>

  9. 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() AscentingCharacters ( A -> Z ) • krsort () DescentingCharacters ( Z -> A ) • asort() AscentingNumbers (1 -> 9 ) • arsort() DescentingNumbers ( 9 -> 1 )

  10. Ksort () Ascenting( A -> Z ) <?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>"; } ?>

  11. krsort () Descenting ( Z -> A ) <?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>"; } ?>

  12. asort() AscentingNumbers ( 1 -> 9 ) <?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>"; } ?>

  13. arsort() DescentingNumbers ( 9 -> 1 ) <?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>"; } ?>

  14. THE END

More Related