PHP array learning

Source: Internet
Author: User
PHP array learning what is an array?
The data types that can be used to store multiple data are arrays.
In php, arrays are the set of keywords and values. we can create them in this way.
① Create an array
Method 1:
$ Arr [0] = 123;
[0] is called a subscript or a keyword.
$ Arr [0] is an element of an array.
$ Arr [0] = 123 indicates the value corresponding to the $ arr [0] element.
$ Arr is the name of this array.
In php, the value stored by the element can make any data type
Method 2:
Basic syntax
Direct assignment
$ Array name = array (value ,......);
Example:
$ Arr = array (89.5, "hello );
Method 3: By default, our subscript starts from 0, but you can also specify it yourself.
Basic syntax
$ Arr ["logo"] = "Beijing ";
$ Arr ["MK"] = 123;
......
Or
$ Arr = array ("logo" => "Beijing", "MK" => 123,4 => 678 );

*********************************
$ Arr = array (89.5, "hello );
① If a subscript is not specified for an element when we create an array, php will automatically use the largest value (integer ), add a subscript (keyword) to the element)
② If we give an element the same subscript, it will overwrite the original value (replace)
Use TRUE as the key name to make integer1 a key name. Use FALSE as the key name to make integer0 the key name.
Using NULL as the key name is equivalent to using a NULL string. Use an empty string as the key name to create (or overwrite) a new value with an empty string as the key name, which is different from the empty square brackets.
Using decimals as the key will automatically cut off the fractional part:
$ A = arr ("abc" => 123, "hhh" => 456, 12.6 => "hello ");
Print_r ($ a); // The Display information is:
Array ([abc] => 123 [hhh] => 456 [12] => hell3o) cannot use arrays and objects as keys ). This will cause a warning: Illegal offset type.
③ If we use true, false, and null as the key, access method:
$ Arr [true] = "hello ";
$ Arr [false] = "world ";
$ Arr [null] = "Beijing ";
Echo $ arr [0];
Echo"
". $ Arr [1];
Echo"
". $ Arr [null];
Echo"
". $ Arr [" "];
?>
④ Do not cross-border the array of methods. For example:
$ Arr = array (56,100,101 );
Echo $ arr [3];
************************
$ A = arr (2, 3 );
$ A [2] = 56;
Echo $ a [2]; // The printed result is 56.
?>
Note: PHP arrays can grow dynamically.
$ Array name [key value]
If the key value you wrote does not exist, it will report: Notice: Undefined offset
************************
One-dimensional array reference trap
$ Arr [bar] = "hello, world"; // PHP considers bar as a constant.
Echo $ arr [bar];
A warning is reported, so it is dangerous to use const bar = "yy.
☆★PHP array-related system functions
Count () function
① Count ($ array name); count array entries
② The is_array () function judges the array
$ Arr = arrays ("12", "345 ");
// $ Arr = 1;
Echo is_array ($ arr );
③ Print_r () and var_dump [displaying data types at the same time] can display Array Information
④ Split the string explode ("What to split ")
$ Str = "Beijing, Tianjin, Songjiang ";
// This method can be used to split strings in actual development.
$ Arr = explode ("", $ str );
Print_r ($ arr );
⑤ The sort function is used for sorting
$ A [0] = 1l
$ A [1] = 3;
$ A [2] = 5;
$ Result = count ($ );
// Result = 3
$ B [0] = 7;
$ B [5] = 9;
$ B [10] = 11;
// $ Result = 3
$ Arr = array ();
Echo count ($ arr );
// Result = 0
?>
Traverse one-dimensional arrays
There are many ways to traverse arrays. you can use the for loop and while loop. the most ingenious way to traverse arrays is foreach ().
Foreach ($ arr as $ key => $ value)
{
......
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.