SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding PHP Arrays: Types, Accessing, and Sorting Methods

shalin avatar
14 diapositives · 329 Views · Sep 02, 2014

This guide provides a comprehensive overview of PHP arrays, including their definition, types, and how to access their elements. Arrays in PHP can hold multiple values under a single variable name, making data management easier. We delve into three main types of arrays: numeric, associative, and multidimensional. Additionally, we discuss array sorting methods like `ksort()`, `krsort()`, `asort()`, and `arsort()` to arrange data. Examples are included to illustrate how to create, access, and sort arrays effectively in PHP.

Understanding PHP Arrays: Types, Accessing, and Sorting Methods - slide 1
01 / 14Understanding PHP Arrays: Types, Accessing, and Sorting Methods
1 / 17

Télécharger la présentation

Understanding PHP Arrays: Types, Accessing, and Sorting Methods
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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