PHP Array Syntax: Create an Array
language-construct array () is used to create an array in PHP. See an example
array ([key =>] value
...
)
key: key can be an integer or string
value: A value can be any PHP type
Examples
$ Arr = array ("foo" => "bar", 12 => true);
echo $ arr ["foo"]; this will print bar
echo $ arr [12], this will print a
$ Arr = array (5 => 1, 12 => 2); This will create an array with 2 elements
$ Arr [] = 56; new key will be maximum ie a key + $ arr [13] = 56
$ Arr ["x"] = 42;
array (5 => 43, 32, 56, "b" => 12); The Same This array is the following.
array (5 => 43, 6 => 32, 7 => 56, "b" => 12);
HTML form with array
<input type="checkbox" name="selected_ids[]" value="1">
<input type="checkbox" name="selected_ids[]" value="2">
<input type="checkbox" name="selected_ids[]" value="3">
<input type="checkbox" name="selected_ids[]" value="11">
<input type="checkbox" name="selected_ids[]" value="12">
<input type="checkbox" name="selected_ids[]" value="13">
foreach ($ _POST ['selected_ids'] as $ key => $ value) {
echo "Key: $ key, Value: $ value <br>";
}
for example, 1.2 and 12 are selected from the HTML form above then above code will print
Key: 0 Value: 1
Key: 1 Value: 2
Key: 2 Value: 12
Popularity: 17% [ ? ]
Related posts Brought to you by Yet Another Related Posts Plugin .









