How to Build a PHP Array
Arrays are a method of storing and indexing multiple pieces of information. Unlike a standard variable, a PHP array is like a filing cabinet. Each element of the array has a key that identifies it to your code. This index can be a number or a text label to help make the purpose of the value clearer to human readers. You can build a PHP array with a single line of code.
Instructions
-
-
1
Create a new PHP file in your preferred text editor, or launch Windows Notepad from the Start menu. It is important to use a plain text editor, as PHP files cannot be read if they contain word processor formatting.
-
2
Place the text "<?php" without quotation marks on the first line of the document. Press "Enter" a few times, then type "?>" to create the document closing tag.
-
-
3
Type the following on its own line between the opening and closing tags:
$myArray = array("firstlabel" => "first element", "secondlabel" => 3, "thirdlabel" => true);
-
4
Modify each value to hold the information you would like to store in the array. In the previous example, "firstlabel," "secondlabel" and "thirdlabel" are keys, while "first element," "3" and "true" are contained within them.
-
5
Save your document. When you run your code, the array will be constructed using the values you defined.
-
1