1 / 9

PHP Arrays

PHP Arrays. Numerically Indexed Arrays Example 6-1. Adding items to an array < ? php $ paper [] = " Copier "; $ paper [] = " Inkjet "; $ paper [] = "Laser"; $ paper [] = " Photo "; print_r ($paper); ? >. Basic Access.

oral
Download Presentation

PHP Arrays

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

  2. Numerically Indexed Arrays Example 6-1. Adding items to an array <?php $paper[] = "Copier"; $paper[] = "Inkjet"; $paper[] = "Laser"; $paper[] = "Photo"; print_r($paper); ?> Basic Access

  3. Example 6-4. Adding items to an associative array and retrieving them <?php $paper['copier'] = "Copier & Multipurpose"; $paper['inkjet'] = "Inkjet Printer"; $paper['laser'] = "Laser Printer"; $paper['photo'] = "PhotographicPaper"; echo $paper['laser']; ?> AssociativeArrays

  4. Example 6-5. Adding items to an array using the array keyword <?php $p1 = array("Copier", "Inkjet", "Laser", "Photo"); echo "p1 element: " . $p1[2] . "<br>"; $p2 = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "PhotographicPaper"); echo"p2 element: " . $p2['inkjet'] . "<br>"; ?> Assignment Using the array Keyword

  5. Example 6-6. Walking through a numeric array using foreach...as <?php $paper = array("Copier", "Inkjet", "Laser", "Photo"); $j = 0; foreach($paper as $item) { echo "$j: $item<br>"; ++$j; } ?> The foreach...as Loop

  6. A simple design feature in PHP’s array syntax makes it possible to create arrays of more than one dimension • Example 6-10. Creating a multidimensional associative array MultidimensionalArrays

  7. is_array() echo (is_array($fred)) ? "Is an array" : "Is not an array"; count() • To count all the elements in the top level of an array, use a command such as the following: echo count($fred); Using Array Functions

  8. sort() sort($fred); shuffle() shuffle($cards); explode() <?php $temp = explode(' ', "This is a sentence with seven words"); print_r($temp); ?>

  9. extract() • compact() • reset()

More Related