PHP Tutorials - Herong's Tutorial Examples - v5.18, by Herong Yang
Usage Examples of Array Functions
This section provides usage examples of some commonly used array functions.
Here a PHP script that provides usage examples of some commonly used array functions:
<?php # ArrayTips.php #- Copyright 2003 (c) HerongYang.com. All Rights Reserved. # print("Empty array:\n"); $emptyArray = array(); print_r($emptyArray); print("\n"); print("Array with default keys:\n"); $indexedArray = array("PHP", "Perl", "Java"); print_r($indexedArray); print("\n"); print("Array with specified keys:\n"); $mappedArray = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); print_r($mappedArray); print("\n"); print("Array with default keys:\n"); $indexedArray = array("PHP", "Perl", "Java"); $value = $indexedArray["1"]; print("The second value: $value\n"); print("\n"); print("Array with specified keys:\n"); $mappedArray = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); $value = $indexedArray["2"]; print("The third value: $value\n"); print("\n"); $languages = array(); $languages["Zero"] = "PHP"; $languages["One"] = "Perl"; $languages["Two"] = "Java"; print("Array with inserted values:\n"); print_r($languages); print("\n"); $mixed = array(); $mixed["Zero"] = "PHP"; $mixed[1] = "Perl"; $mixed["Two"] = "Java"; $mixed["3"] = "C+"; $mixed[""] = "Basic"; print("Array with mixed keys:\n"); print_r($mixed); print("\$mixed[3] = ".$mixed[3]."\n"); print("\$mixed[\"3\"] = ".$mixed["3"]."\n"); print("\$mixed[\"\"] = ".$mixed[""]."\n"); print("\n"); $mixed = array(); $mixed["Zero"] = "PHP"; $mixed[1] = "Perl"; $mixed["Two"] = "Java"; $mixed["3"] = "C+"; $mixed[""] = "Basic"; $mixed[] = "Pascal"; $mixed[] = "FORTRAN"; print("Array with default keys:\n"); print_r($mixed); print("\n"); $oldArray = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); $newArray = $oldArray; $refArray = <$oldArray; $newArray["One"] = "Python"; $refArray["Two"] = "C#"; print("\$newArray[\"One\"] = ".$newArray["One"]."\n"); print("\$oldArray[\"One\"] = ".$oldArray["One"]."\n"); print("\$refArray[\"Two\"] = ".$refArray["Two"]."\n"); print("\$oldArray[\"Two\"] = ".$oldArray["Two"]."\n"); print("\n"); $array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); $array["3"] = "C+"; $array[""] = "Basic"; $array[] = "Pascal"; $array[] = "FORTRAN"; print("Loop on value only:\n"); foreach ($array as $value) { print("$value, "); } print("\n\n"); print("Loop on key and value:\n"); foreach ($array as $key=>$value) { print("[$key] => $value\n"); } print("\n"); $mixed = array(); $mixed["Two"] = "Java"; $mixed["3"] = "C+"; $mixed["Zero"] = "PHP"; $mixed[1] = "Perl"; $mixed[""] = "Basic"; $mixed[] = "Pascal"; $mixed[] = "FORTRAN"; $mixed["Two"] = ""; unset($mixed[4]); print("Order of array values:\n"); print_r($mixed); print("\n"); $var = array(0,0,7); print("Test 1: ". is_array($var)."\n"); $var = array(); print("Test 2: ". is_array($var)."\n"); $var = 1800; print("Test 3: ". is_array($var)."\n"); $var = true; print("Test 4: ". is_array($var)."\n"); $var = null; print("Test 5: ". is_array($var)."\n"); $var = "PHP"; print("Test 6: ". is_array($var)."\n"); print("\n"); $array = array("PHP", "Perl", "Java"); print_r("Size 1: ".count($array)."\n"); $array = array(); print_r("Size 2: ".count($array)."\n"); print("\n"); $array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); print("Is 'One' defined? ".array_key_exists("One", $array)."\n"); print("Is '1' defined? ".array_key_exists("1", $array)."\n"); print("Is 'Two' defined? ".isset($array["Two"])."\n"); print("Is '2' defined? ".isset($array[2])."\n"); print("\n"); $array = array("Perl", "PHP", "Java", "PHP"); print("Search 1: ".array_search("PHP",$array)."\n"); print("Search 2: ".array_search("Perl",$array)."\n"); print("Search 3: ".array_search("C#",$array)."\n"); print("\n"); $mixed = array(); $mixed["Zero"] = "PHP"; $mixed[1] = "Perl"; $mixed["Two"] = "Java"; $mixed["3"] = "C+"; $mixed[""] = "Basic"; $mixed[] = "Pascal"; $mixed[] = "FORTRAN"; $keys = array_keys($mixed); print("Keys of the input array:\n"); print_r($keys); print("\n"); $mixed = array(); $mixed["Zero"] = "PHP"; $mixed[1] = "Perl"; $mixed["Two"] = "Java"; $mixed["3"] = "C+"; $mixed[""] = "Basic"; $mixed[] = "Pascal"; $mixed[] = "FORTRAN"; $values = array_values($mixed); print("Values of the input array:\n"); print_r($values); print("\n"); $mixed = array(); $mixed["Zero"] = "PHP"; $mixed[1] = "Perl"; $mixed["Two"] = "Java"; $mixed["3"] = "C+"; $mixed[""] = "Basic"; $mixed[] = "Pascal"; $mixed[] = "FORTRAN"; sort($mixed); print("Sorted by values:\n"); print_r($mixed); print("\n"); $mixed = array(); $mixed["Zero"] = "PHP"; $mixed[1] = "Perl"; $mixed["Two"] = "Java"; $mixed["3"] = "C+"; $mixed[""] = "Basic"; $mixed[] = "Pascal"; $mixed[] = "FORTRAN"; ksort($mixed); print("Sorted by keys:\n"); print_r($mixed); print("\n"); $old = array(); $old["Zero"] = "PHP"; $old[1] = "Perl"; $old["Two"] = "Java"; $old["3"] = "C+"; $old[""] = "Basic"; $old[] = "Pascal"; $old[] = "FORTRAN"; $keys = array_keys($old); $values = array_values($old); print("Combined:\n"); $new = array_combine($keys, $values); print_r($new); print("\n"); print("Combined backward:\n"); $new = array_combine($values, $keys); print_r($new); print("\n"); $array = array("Google", "Yahoo", "Netscape"); list($first, $second, $third) = $array; print("Test 1: The third site = $third\n"); # split() was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0 # list($month, $date, $year) = split("/","1/1/2006"); list($month, $date, $year) = explode("/","1/1/2006"); print("Test 2: Year = $year\n"); $array = array("Zero"=>"PHP", 1=>"Basic", "One"=>"Perl", 0=>"Pascal", 2=>"FORTRAN", "Two"=>"Java"); list($first, $second, $third) = $array; print("Test 3: The third language = $third\n"); print("\n"); $lang = array("Perl", "PHP", "Java",); $os = array("i"=>"Windows", "ii"=>"Unix", "iii"=>"Mac"); $mixed = array_merge($lang, $os); print("Merged:\n"); print_r($mixed); print("\n"); $waitingList = array(); array_push($waitingList, "Jeo"); array_push($waitingList, "Leo"); array_push($waitingList, "Kim"); $next = array_shift($waitingList); array_push($waitingList, "Kia"); $next = array_shift($waitingList); array_push($waitingList, "Sam"); print("Current waiting list:\n"); print_r($waitingList); print("\n"); $waitingList = array(); array_push($waitingList, "Jeo"); array_push($waitingList, "Leo"); array_push($waitingList, "Kim"); $next = array_pop($waitingList); array_push($waitingList, "Kia"); $next = array_pop($waitingList); array_push($waitingList, "Sam"); print("Current waiting list:\n"); print_r($waitingList); print("\n"); # each() was DEPRECATED as of PHP 7.2.0 # $array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); # print("Loop with each():\n"); # reset($array); # while (list($key, $value) = each($array)) { # print("[$key] => $value\n"); # } # print("\n"); print("Loop with current():\n"); reset($array); while ($value = current($array)) { print("$value\n"); next($array); } print("\n"); print("Integers:\n"); $integers = range(1, 20, 3); print_r($integers); print("\n"); print("Characters:\n"); $characters = range("X", "c"); print_r($characters); print("\n"); $array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); $array = array_pad($array, 6, ">>"); $array = array_pad($array, -8, "---"); print("Padded:\n"); print(join(",", array_values($array))); print("\n\n"); $array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); $removed = array_splice($array, -2, 2); print("Remaining chunk:\n"); print_r($array); print("\n"); print("Removed chunk:\n"); print_r($removed); print("\n"); $array = array(5, 7, 6, 2, 1, 3, 4, 2); print("Minimum number: ".min($array)."\n"); print("Maximum number: ".max($array)."\n"); $array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java"); print("Minimum string: ".min($array)."\n"); print("Maximum string: ".max($array)."\n"); print("\n"); $array = array("Hello!", "Hi!", "Allo!", "Hallo!", "Coucou!"); $key = array_rand($array); print("Random greeting: ".$array[$key]."\n"); print("\n"); ?>
Output of the above script:
herong> \php\php ArrayTips.php Empty array: Array ( ) Array with default keys: Array ( [0] => PHP [1] => Perl [2] => Java ) Array with specified keys: Array ( [Zero] => PHP [One] => Perl [Two] => Java ) Array with default keys: The second value: Perl Array with specified keys: The third value: Java Array with inserted values: Array ( [Zero] => PHP [One] => Perl [Two] => Java ) Array with mixed keys: Array ( [Zero] => PHP [1] => Perl [Two] => Java [3] => C+ [] => Basic ) $mixed[3] = C+ $mixed["3"] = C+ $mixed[""] = Basic Array with default keys: Array ( [Zero] => PHP [1] => Perl [Two] => Java [3] => C+ [] => Basic [4] => Pascal [5] => FORTRAN ) $newArray["One"] = Python $oldArray["One"] = Perl $refArray["Two"] = C# $oldArray["Two"] = C# Loop on value only: PHP, Perl, Java, C+, Basic, Pascal, FORTRAN, Loop on key and value: [Zero] => PHP [One] => Perl [Two] => Java [3] => C+ [] => Basic [4] => Pascal [5] => FORTRAN Order of array values: Array ( [Two] => [3] => C+ [Zero] => PHP [1] => Perl [] => Basic [5] => FORTRAN ) Test 1: 1 Test 2: 1 Test 3: Test 4: Test 5: Test 6: Size 1: 3 Size 2: 0 Is 'One' defined? 1 Is '1' defined? Is 'Two' defined? 1 Is '2' defined? Search 1: 1 Search 2: 0 Search 3: Keys of the input array: Array ( [0] => Zero [1] => 1 [2] => Two [3] => 3 [4] => [5] => 4 [6] => 5 ) Values of the input array: Array ( [0] => PHP [1] => Perl [2] => Java [3] => C+ [4] => Basic [5] => Pascal [6] => FORTRAN ) Sorted by values: Array ( [0] => Basic [1] => C+ [2] => FORTRAN [3] => Java [4] => PHP [5] => Pascal [6] => Perl ) Sorted by keys: Array ( [] => Basic [Two] => Java [Zero] => PHP [1] => Perl [3] => C+ [4] => Pascal [5] => FORTRAN ) Combined: Array ( [Zero] => PHP [1] => Perl [Two] => Java [3] => C+ [] => Basic [4] => Pascal [5] => FORTRAN ) Combined backward: Array ( [PHP] => Zero [Perl] => 1 [Java] => Two [C+] => 3 [Basic] => [Pascal] => 4 [FORTRAN] => 5 ) Test 1: The third site = Netscape Test 2: Year = 2006 Test 3: The third language = FORTRAN Merged: Array ( [0] => Perl [1] => PHP [2] => Java [i] => Windows [ii] => Unix [iii] => Mac ) Current waiting list: Array ( [0] => Kim [1] => Kia [2] => Sam ) Current waiting list: Array ( [0] => Jeo [1] => Leo [2] => Sam ) Loop with current(): PHP Basic Perl Pascal FORTRAN Java Integers: Array ( [0] => 1 [1] => 4 [2] => 7 [3] => 10 [4] => 13 [5] => 16 [6] => 19 ) Characters: Array ( [0] => X [1] => Y [2] => Z [3] => [ [4] => \ [5] => ] [6] => ^ [7] => _ [8] => ` [9] => a [10] => b [11] => c ) Padded: ---,---,PHP,Perl,Java,>>,>>,>> Remaining chunk: Array ( [Zero] => PHP ) Removed chunk: Array ( [One] => Perl [Two] => Java ) Minimum number: 1 Maximum number: 7 Minimum string: Java Maximum string: Perl Random greeting: Hello!
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Array Related Built-in Functions
►Usage Examples of Array Functions
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
Functions to Manage Directories, Files and Images
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS