PHP Learning Path----arrays, multidimensional arrays, array functions

Source: Internet
Author: User

PHP Arrays


array Basic writing format:
1: Simple form Array ("AA", 12,true,2.2, "test", ";");
2: Full form Array ("title" = "AA", "Age" =>20); This format creates a key name for an array, and if you create a key name, you cannot pass subscript 0,1,2 ... To access the array elements


up.
3: $arr [0]= '; $arr [1]= ' Hello World ';






array creation, modification, deletion, use


modifying an array element
$arr =array (11,22,33,44);
$arr [0] = 66; the array becomes $arr=array (66,22,33,44);
To Delete an array element
$arr = Array (11,22,33,44);
unset ($arr [0]); the array becomes $arr = array (22,33,44);
access to a single element, either by subscript or by key name
$arr = Array (11,22,33,44);
echo $arr [0]; a value of one------for a simple array, you can use the subscript to access
$arr = Array (' A ' =>11, ' B ' =>22, ' C ' =>33);
echo $arr [' B ']; The value is-----for this array, there is no way to access the array element by subscript




traversal of an array
The so-called traversal of an array is the array of elements according to the requirements of printing or to get the relevant values. We can do this with a very nice function, foreach, provided after php5, in a format such as


A.
foreach ($arr as $key = = $val) {
[related $key or $val content]
}
$arr-the array to be accessed $key the key name of---array or the variable stored in the subscript $val--the variable stored by the key value of the array


traversal of an array
$info = Array ("A" = "php", "B" = "+", "c" = "com");
foreach ($info as $value) {
echo $value. " <br> ";
}
This traversal format simply accesses the array's key values, ignoring the array's key names.
echo "
foreach ($info as $id = = $value) {
echo $id. " ---". $value." <br> ";
}
This traversal allows access to the array's key names and key values.




It is worth noting that foreach can theoretically traverse only one-dimensional arrays.








two-dimensional arrays


two-dimensional arrays or multidimensional arrays in fact, we can understand that the array contains the value of the array type again, and through the same idea to access. The following constructs a two-dimensional array.
$arr = Array ("P", "PP", "PPP"), Array ("H", "HH", "HHH"));
echo $arr [HHH]; the printed value is




Here are a few examples


$arr = Array (1,2,3,4,5);
the function for printing an array has var_dump,print_r
Var_dump will display the data type of the key value after printing, Print_r will not display the data type of the key value after printing.


The default subscript for a generic array starts with 0, and you want to write this in the following format.
$arr = Array ("A", "V", "C", "6");
If we want the subscript of the array to start with the default of 4, it is only necessary to declare the array.
$arr = Array (4=> "a", "V", "C", "6"), the following subscript starts at 4, and the subscript is 4,5,6,7.
We can also declare arrays like this
$arr = Array (4=> "a", "V",45=> "C", "6"); In doing so, the subscript is followed by 4,5,45,46.


OK, let's then declare an array and examine the array's subscript changes after deleting the data element.
$arr = Array ("A", "B", "C", "D");
unset ($array [3]); The print key is named 0,1,2.
unset ($array [1]); The print key is named 0,2,3.


Let's walk through the two-dimensional array, if we're sure it's a two-dimensional array.
declaring a two-dimensional array
$arr = Array (Array ("4", "444"), "2", "7", "8", "6");


foreach ($arr as $key 1=> $value 1) {
if (Is_array ($value 1)) {
foreach ($value 1 as $key 2=> $value 2) {
echo $value 2. " <br> ";
    }
}else{
echo $value 1. " <br> ";
  }
}


so that we can traverse the two-dimensional array.


Sorting of Arrays
$arr = Array (4,2,6,1);
sort the key values from small to large: sort ($arr);
sort key values from large to small: Rsort ($arr);


sort the key names of an array---Note that uppercase letters are preceded by lowercase letters
$arr = Array ("A" =>4, "a" =>2, "C" =>6);
Ksort ($arr);
Krsort ($arr);


the function count ($arr) that counts the number of elements in the array;


the function of the array and the Array_sum ($arr);---result is


























































































































































 

PHP Learning Path----arrays, multidimensional arrays, array functions

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.