Home > Article > Backend Development > Explain the methods of PHP array classification and array creation
Array classification and array creation play an important role in PHP.
1. Array classification
1. Index array
Array whose index value is an integer
2.Associative array
Index value For arrays of strings, use strings as indexes, which makes programming more user-friendly!
This is very rare in other programming languages, but it will be widely used in PHP during the development process.
It is extremely convenient to use!
2. Array creation
Creating arrays in PHP is very flexible. Unlike many other programming languages, PHP does not need to
specify the size of the array when creating it. You can store any type of data in the same array without even declaring it before using it.
. You can create an array by directly assigning values to the array elements.
. Use the array() language structure to create an array.
1. Create an array by directly assigning values to the array elements
Variable name [index value] = data content; the index value can be an integer or a string, or it must be written (default For the index array) 2. Use the array() language structure to create the array variable name =array(key1=>value1,...);
<!--?php /* 创建数组方法一 */ //$student[索引值]=具体的值 $student[0]=10; $student[1]='傻逼'; $student[2]=true; $student[3]=60.5; $student[3]='单位取得完全';
//需要使用print_r()函数来输出数组的具体内容 //print_r($student); var_dump($student); $student1['num']=10; $student1['name']='傻逼'; $student1['sex']=true; $student1['grade']=60.5; var_dump($student1); //使用数组里面具体数据的方法 //数组变量名称[索引值]; echo $student[1];
<!--?php /*
Three methods of creating an array
*/ //$student=array(索引值=-->具体的值,.......); $student=array(10,'傻逼',true,60.5);//一维数组 var_dump($student); $student1=array( 0=>10, 1=>'傻逼', 2=>true, 3=>60.5 ); var_dump($student1); $student2=array( 'num'=>11, 'name'=>'菜逼', 'sex'=>true, 'grade'=>80.5, 10=>'dqwdwqdwq' ); var_dump($student2); ?>
<!--?php //二维数组,多维数组 $students=array( 0=-->array(1,'傻逼',true,60.5), 1=>array(2,'菜逼',true,80.5), 2=>array(3,'坑逼',false,85.5) ); /*
$students=array( array(1,'傻逼',true,60.5), array(2,'菜逼',true,80.5), array(3,'坑逼',false,85.5) ); */ var_dump($students); echo $students[0][1]; ?>
This article explains PHP array classification and array creation methods. For more related content, please pay attention to the PHP Chinese website.
Related recommendations:
Explanation on PHP language tags, command delimiters, and comments
##Explain the predefined super-global array variables of PHP arrays
Explain the use of pdo placeholders in PHP
The above is the detailed content of Explain the methods of PHP array classification and array creation. For more information, please follow other related articles on the PHP Chinese website!