Array php snippets


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Building a Multidimensional Array
  2.  
  3. Here is how our $arrBooks example from last week’s article can be expanded into a multidimensional array:
  4.  
  5. <?php
  6. $arrBooks = array(
  7. ‘Comic’ => array(
  8. ‘Title’=>‘Superman’,
  9. ‘Author’=>’Jerry Siegel and Joe Shuster’,
  10. ‘Publication Date’ => ‘1938’),
  11.  
  12. ‘Science Fiction’ => array(
  13. ‘Title’=>‘Dune’,
  14. ‘Author’=>’Frank Herbert’,
  15. ‘Publication Date’=>’1965’),
  16.  
  17. ‘Fantasy’ => array(
  18. ‘Title’=>‘The Hobbit’,
  19. ‘Author’=>’J.R.R. Tolkien’,
  20. ‘Publication Date’=>’1937’),
  21.  
  22. ‘Horror’ => array(
  23. ‘Title’=>‘Carrie’,
  24. ‘Author’=>’Stephen King’,
  25. ‘Publication Date’=>’1974’)
  26. );
  27. ?>
  28.  
  29. Extracting Elements from a Multidimensional Array
  30.  
  31. To extract a single element from the multidimensional array, you must refer to the keys in both the outer and inner arrays. For instance, the PHP code below:
  32.  
  33. <?
  34. echo $arrBooks[‘Science Fiction][‘Title’];
  35. echo "<br>";
  36. echo $arrBooks[‘Horror’][‘Author’];
  37. ?>
  38.  
  39. would display:
  40.  
  41. Dune
  42. Stephen King
  43.  
  44. Looping Through a Multidimensional Array
  45.  
  46. The easiest way to loop through a multidimensional array is to nest two foreach loops; the outer loop goes through each outer array element, and the inner loop goes through each inner array element within the selected outer element.
  47.  
  48. <?
  49. foreach ($arrBooks as $obj_key =>$book)
  50. {
  51. echo "$obj_key Book:<br>";
  52. foreach ($book as $key=>$value){
  53. echo "$key: $value<br>";
  54. }
  55. echo "<br>";
  56. }
  57. ?>
  58.  
  59. The display will look like this:
  60.  
  61. Comic Book:
  62. Title: Superman
  63. Author: Jerry Siegel and Joe Shuster
  64. Publication Date: 1938
  65.  
  66. Science Fiction Book:
  67. Title: Dune
  68. Author: Frank Herbert
  69. Publication Date: 1965
  70.  
  71. Fantasy Book:
  72. Title: The Hobbit
  73. Author: J.R.R. Tolkien
  74. Publication Date: 1937
  75.  
  76. Horror Book:
  77. Title: Carrie
  78. Author: Stephen King
  79. Publication Date: 1974
  80.  
  81. Array Functions
  82.  
  83. Arrays are one of the most useful variable types. Along with its versatility, arrays also can use a variety of functions. In the previous lesson, we used the is_array function to determine if a variable was an array and the sort function to sort the elements of an array. Here are some more examples of array functions.
  84.  
  85. count($array): Counts the number of elements in an array.
  86.  
  87. <?
  88. $numBooks = count($arrBooks);
  89. echo "There are $numBooks books in the collection.<br>";
  90. ?>
  91.  
  92. There are 4 books in the collection.
  93.  
  94. extract($array): Converts associative array keys into string variables. The values of each key become the values of each variable.
  95.  
  96. <?
  97. $arrBooks = array( ‘Comic’ => ‘Superman’,
  98. ‘ScienceFiction’ => ‘Dune’,
  99. ‘Fantasy’ => ‘The Hobbit’,
  100. ‘Horror’ => ‘Carrie’);
  101.  
  102. extract($arrBooks);
  103. // $arrBooks[‘Comic’] becomes $Comic
  104. // $arrBooks[‘ScienceFiction’] becomes $ScienceFiction
  105. // $arrBooks[‘Fantasy] becomes $Fantasy
  106. // $arrBooks[‘Horror] becomes $Horror
  107.  
  108. echo "$Comic is a comic book.<br>";
  109. echo "$Fantasy is a fantasy book.<br>";
  110.  
  111. ?>
  112.  
  113. Superman is a comic book.
  114. The Hobbit is a fantasy book.
  115.  
  116. extract($array, EXTR_PREFIX_ALL, ‘prefix’): Adds a prefix to the string variable to differentiate between arrays that have the same keys.
  117.  
  118. <?
  119. $arrBooks = array(
  120. ‘Comic’ => ‘Superman’,
  121. ‘ScienceFiction’ => ‘Dune’,
  122. ‘Fantasy’ => ‘The Hobbit’,
  123. ‘Horror’ => ‘Carrie’);
  124.  
  125. extract($arrBooks, EXTR_PREFIX_ALL, "books");
  126. // $arrBooks[‘Comic’] becomes $books_Comic
  127. // $arrBooks[‘ScienceFiction’] becomes $books_ScienceFiction
  128. // $arrBooks[‘Fantasy] becomes $books_Fantasy
  129. // $arrBooks[‘Horror] becomes $books_Horror
  130.  
  131. echo "$books_Comic is a comic book.<br>";
  132. echo "$books_Fantasy is a fantasy book.<br>";
  133.  
  134. $arrFilms = array(
  135. ‘Comic’ => ‘Superman Returns’,
  136. ‘ScienceFiction’ => ‘Terminator’,
  137. ‘Fantasy’ => ‘Dark Crystal’,
  138. ‘Horror’ => ‘Friday the 13th’);
  139.  
  140. extract($arrFilms, EXTR_PREFIX_ALL, "films");
  141. // $arrFilms [‘Comic’] becomes $films_Comic
  142. // $arrFilms [‘ScienceFiction’] becomes $films_ScienceFiction
  143. // $arrFilms [‘Fantasy] becomes $films_Fantasy
  144. // $arrFilms [‘Horror] becomes $films _Horror
  145.  
  146. echo "$films_Comic is a comic film.<br>";
  147. echo "$films_Fantasy is a fantasy film.<br>";
  148. ?>
  149.  
  150. Superman is a comic book.
  151. The Hobbit is a fantasy book.
  152. Superman Returns is a comic film.
  153. Dark Crystal is a fantasy film.
  154.  
  155. compact(var1, var2, var3): Converts a list of variables into an array.
  156.  
  157. <?
  158. $Comic = ‘Batman’;
  159. $ScienceFiction = ‘Dreaming Void’;
  160. $Fantasy = ‘American Gods’;
  161. $Horror = ‘Frankenstein’;
  162.  
  163. $arrBooks2 = compact (‘Comic’, ‘ScienceFiction’, ‘Fantasy’, ‘Horror’);
  164.  
  165. foreach ($arrBooks2 as $key => $value) {
  166. print "$value is an example of a $key book.<br>\n";
  167. }
  168. ?>
  169.  
  170. Batman is an example of a Comic book.
  171. Dreaming Void is an example of a ScienceFiction book.
  172. American Gods is an example of a Fantasy book.
  173. Frankenstein is an example of a Horror book.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.