Tools, FAQ, Tutorials:
Data Types of Array Keys in PHP
What Types of Data Can Be Used as Array Keys in PHP?
✍: FYIcenter.com
Two types of data can be used as array keys: string and integer. When a string is used as a key and the string represent an integer, PHP will convert the string into a integer and use it as the key. Here is a PHP script on different types of keys:
<?php
$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");
?>
This script will print:
Array with mixed keys:
Array
(
[Zero] => PHP
[1] => Perl
[Two] => Java
[3] => C+
[] => Basic
)
$mixed[3] = C+
$mixed["3"] = C+
$mixed[""] = Basic
Note that an empty string can also be used as a key.
⇒ Index of Array Values in PHP
⇐ Retrieving Values out of an Array in PHP
2017-02-03, ∼3801🔥, 0💬
Popular Posts:
Where to find EPUB Sample Files? Here is a list of EPUB sample files collected by FYIcenter.com team...
How To Loop through an Array without Using "foreach" in PHP? PHP offers the following functions to a...
Where to get the detailed description of the json_encode() Function in PHP? Here is the detailed des...
How to use "{{...}}" Liquid Codes in "set-body" Policy Statement? The "{{...}}" Liquid Codes in "set...
What's Wrong with "while ($c=fgetc($f)) {}" in PHP? If you are using "while ($c=fgetc($f)) {}" to lo...