
Arrays in PHP: Index Values and Associative Arrays
Learn about arrays in PHP, including indexed lists of elements with numerical or arbitrary index values. Explore associative arrays, keys, printing arrays, and more in this comprehensive guide by Professor John Carelli from Kutztown University's Computer Science Department.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
PHP Arrays CSC 242 Professor John Carelli Kutztown University Computer Science Department
Arrays in PHP PHP supports arrays that consist of an indexed list of elements Two types are supported: Arrays with numerical index values Arrays with arbitrary index values (typically strings) Also referred to as associate arrays Professor John Carelli Kutztown University Computer Science Department
Arrays with Integer Keys Integer index values are placed in square backets following the array base name Using explicit indices: <?php $a[0] = "one"; $a[1] = "two"; $a[2] = "three"; ?> Missing index value creates a new index at the end (i.e., push operation): <?php $a[] = "one"; $a[] = "two"; $a[] = "three"; ?> Professor John Carelli Kutztown University Computer Science Department
Associative Array String index values are placed in square backets following the array base name (in quotes) Resulting in a mapping of key / value pairs <?php $b['one'] = 1; $b['two'] = 2; $b['three'] = 3; ?> If multiple elements are declared with the same key, then only the last value is retained (previous value is overwritten) Professor John Carelli Kutztown University Computer Science Department
More on Keys Associative arrays a string containing a valid integer will be cast to an integer floats and bools get cast to integers (floats get truncated) Null value gets cast to an empty string Arrays and objects cannot be used as keys Professor John Carelli Kutztown University Computer Science Department
Printing Arrays print_r() print an entire array: print_r($MyArray); Outputs a human-readable representation When used in HTML, enclose it in <pre> tags to make it pretty (otherwise, the whitespace in the output will get collapsed) An array element can be used as a variable in a string interpolation echo "$var[0]"; however, Professor John Carelli Kutztown University Computer Science Department
Printing Arrays with string keys If the key is not an integer, the element must be enclosed in curly braces, or an interpolation error will result. Correct: echo "${var['key']}"; Incorrect, without curly braces: php > echo "$var['key']"; PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in php shell code on line 1 Professor John Carelli Kutztown University Computer Science Department
array keyword Define an array with the array keyword Numerical index: $a = array("one", "two", "three"); Associative array: $b = array( 'one' => 1, 'two' => 2, 'three' => 3); Professor John Carelli Kutztown University Computer Science Department
foreach loop Theforeach loop can be used to iterate through the values of an array Arrays with integer keys: foreach($a as $element) { echo "$element"; } Associative arrays: foreach($b as $key => $value) { echo "$key: $value"; } Source: Dylan Schwesinger Kutztown University Computer Science Department
Multidimensional Arrays The value of a key can be an array $tic_tac_toe = array( array('X', ' ', 'O'), array('O', 'O', 'X'), array('X', 'O', ' ') ); Source: Dylan Schwesinger Kutztown University Computer Science Department
Useful built-in Array Functions is_array will return true if a variable is an array count will return the number of elements in an array sort performs an in-place sorting of array elements explode converts a string into an array implode converts an array into a string array_keys returns an array containing the keys of an array array_values returns an array containing the elements of an array unset removes an element from an array Professor John Carelli Kutztown University Computer Science Department
PHP Examples Arrays & Files firstArray.php arrayExamples.php sortArrays.php Professor John Carelli Kutztown University Computer Science Department
Superglobals Predefined variables that are provided by the PHP environment $_GET: variables passed to the current script via the HTTP GET method $_POST: variables passed to the current script via the HTTP POST method $_COOKIE: variables passed to the current script via HTTP cookies $_SESSION: session variables available to the current script Professor John Carelli Kutztown University Computer Science Department