140 likes | 298 Views
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.
E N D
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.
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
PHP Arrays Objects (con.) EX: <?php $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
Example read array by call text <?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?>
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>"; } ?>
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]; ?>
Example using array foreach loop <?php $colors = array("red","green","blue","yellow"); foreach ($colors as $value) { echo "$value <br>"; } ?>
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 )
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>"; } ?>
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>"; } ?>
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>"; } ?>
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>"; } ?>