



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Chapter 6 Learning PHP, MySQL & JavaScript_ with jQuery, CSS & HTML5 (4th ed.) [Nixon 2014- 12-14]
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.
(^) 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, … 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");
(^) 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: $descriptionINDEXED 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); }
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";
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);
rsort(): (^) You can also sort an array in reverse order using the rsort function, like this: rsort($fred, SORT_NUMERIC); rsort($fred, SORT_STRING);
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.
(^) 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 (^) )
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);