Home > Backend Development > PHP Tutorial > Explain the methods of PHP array classification and array creation

Explain the methods of PHP array classification and array creation

jacklove
Release: 2023-03-30 17:38:01
Original
1770 people have browsed it

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]=&#39;傻逼&#39;;
$student[2]=true;
$student[3]=60.5;
$student[3]=&#39;单位取得完全&#39;;
Copy after login
//需要使用print_r()函数来输出数组的具体内容
//print_r($student);
var_dump($student);
$student1[&#39;num&#39;]=10;
$student1[&#39;name&#39;]=&#39;傻逼&#39;;
$student1[&#39;sex&#39;]=true;
$student1[&#39;grade&#39;]=60.5;
var_dump($student1);
//使用数组里面具体数据的方法
//数组变量名称[索引值];
echo $student[1];
<!--?php
/*
Copy after login

Three methods of creating an array

*/
//$student=array(索引值=-->具体的值,.......);
 $student=array(10,&#39;傻逼&#39;,true,60.5);//一维数组 
 var_dump($student); 
 $student1=array( 0=>10, 1=>&#39;傻逼&#39;, 2=>true, 3=>60.5 ); 
 var_dump($student1);
  $student2=array( &#39;num&#39;=>11, &#39;name&#39;=>&#39;菜逼&#39;, &#39;sex&#39;=>true, &#39;grade&#39;=>80.5, 10=>&#39;dqwdwqdwq&#39; ); 
  var_dump($student2); ?>
Copy after login
<!--?php
//二维数组,多维数组
$students=array(
    0=-->array(1,&#39;傻逼&#39;,true,60.5),
    1=>array(2,&#39;菜逼&#39;,true,80.5),
    2=>array(3,&#39;坑逼&#39;,false,85.5)
);
/*
Copy after login
$students=array(
        array(1,&#39;傻逼&#39;,true,60.5),
        array(2,&#39;菜逼&#39;,true,80.5),
        array(3,&#39;坑逼&#39;,false,85.5)
);
*/ 
var_dump($students);
echo $students[0][1];
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template