Docsity

PHP Arrays: Creating and Accessing Numeric and Associative Arrays, Essays (university) of Programming Methodologies

Partial preview of the text

Download PHP Arrays: Creating and Accessing Numeric and Associative Arrays and more Essays (university) Programming Methodologies in PDF only on Docsity!

PHP ARRAYS

Chapter 6 Learning PHP, MySQL & JavaScript_ with jQuery, CSS & HTML5 (4th ed.) [Nixon 2014- 12-14]

ARRAYS BASIC ACCESS

 Some arrays are referenced by numeric indices; others allow alphanumeric identifiers (associative arrays).  Built-in functions let you sort them, add or remove sections, and walk through them to handle each item through a special kind of loop.  (^) And by placing one or more arrays inside another, you can create arrays of two, three, or any number of dimensions.

ASSOCIATIVE ARRAYS

 (^) you can reference the items in an array by name rather than by number.

Use the words key and value to access associative arrays Key: name, ink , … Value: Copier & Multipurpose, Inkjet Printer, …

ASSIGNMENT USING THE ARRAY

KEYWORD

 A more compact and faster assignment method uses the array keyword. $p1 = array("Copier", "Inkjet", "Laser", "Photo"); $p2 = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper");

THE FOREACH...AS LOOP

"Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"); foreach($paper as $item => $description) // agin // foreach($paper as $description) // and this echo "$item: $description
"; ?>

LIST FUNCTION

 (^) As an alternative syntax to foreach...as, you can use the list function in conjunction with the each function,

"Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"); while (list($item, $description) = each($paper)) echo "$item: $description
"; ?>  (^) a while loop is set up and will continue looping until each returns a value of FALSE.  (^) The each function acts like foreach: it returns an array containing a key/value pair from the array $paper and then moves its built-in pointer to the next  (^) pair in that array.

INDEXED FOR LOOP (WHILE) TO ACCESS ASSOCIATIVE ARRAY  reset(), next(), key(), end(), prev(), current(), $third[ "ArtTic" ] = 21; $third[ "LunaTic" ] = 18; $third[ "GalAnt" ] = 23; // iterate through the array elements and print each // element’s name and value for ( reset( $third ); $element = key( $third ); next( $third ) ) print( "$element is $third[$element]
" );

INDEXED FOR LOOP (WHILE) TO ACCESS ASSOCIATIVE ARRAY  (^) <?php $array = array( '0' => '5', '1' => '2', '2' => '0', '3' => '3', '4' => '1'); // is it correct approach? while ($fruit_name = current($array)) { echo key($array).'
'; next($array); }

USING ARRAY FUNCTIONS

 PHP comes with numerous other functions for handling arrays. The full list is at http:// php.net/manual/en/ref.array.php  However, some of these functions are so fundamental that it’s worth taking the time  is_array():  (^) Finds whether a variable is an array  (^) Returns true is the parameter (variable being evaluated) is an array, false otherwise. echo (is_array($fred))? "Is an array" : "Is not an array";

USING ARRAY FUNCTIONS

 count():  (^) When you need to know exactly how many elements there are in your array, particularly if you will be referencing them directly. echo count($fred);

USING ARRAY FUNCTIONS

 rsort():  (^) You can also sort an array in reverse order using the rsort function, like this: rsort($fred, SORT_NUMERIC); rsort($fred, SORT_STRING);

USING ARRAY FUNCTIONS

 shuffle():  (^) There may be times when you need the elements of an array to be put in random order, such as when you’re creating a game of playing cards: shuffle($cards);  (^) Like sort, shuffle acts directly on the supplied array and returns TRUE on success or FALSE on error.

USING ARRAY FUNCTIONS

 (^) explode():  (^) <?php  (^) $temp = explode(' ', "This is a sentence with seven words");  (^) print_r($temp);  (^) ?>  (^) Array  (^) (  (^) [0] => This  (^) [1] => is  (^) [2] => a  (^) [3] => sentence  (^) [4] => with  (^) [5] => seven  (^) [6] => words  (^) )

USING ARRAY FUNCTIONS

 extract():  (^) Sometimes it can be convenient to turn the key/value pairs from an array into PHP variables. One such time might be when you are processing the $_GET or $_POST variables as sent to a PHP script by a form.  extract ($_GET);  extract ($_POST);