Detailed introductory tutorial on php array definition_PHP tutorial

WBOY
Release: 2016-07-13 17:16:22
Original
1165 people have browsed it

There are several ways to define arrays in php, such as array(), or arr[] to implement array definition. Let me introduce to you in detail. Detailed explanation of various techniques of PHP array definition

PHP array is an important concept. It contains a large number of functions to facilitate people's development... Its arrays are now classified to facilitate query and application

Use.
Let’s talk about the definition of PHP array first... PHP array contains two items, key and value. The corresponding value can be obtained through key, and key can be

It is numerical and related, such as $array[0], $array[one]...
Create array
The array declaration in PHP is slightly different from that in other languages, but it can still be declared as one-dimensional, two-dimensional, three-dimensional and multi-dimensional, such as
$array[0] = 1,$array = array(1,2,3); One-dimensional array, including only three values, is a numeric array, and can be referenced with $array

[0] represents 1, you can omit the index when creating a numerical array

Create an array in PHP using the array() structure to define it, such as:

The code is as follows Copy code
 代码如下 复制代码


$people=array('name','sex','nation','brith');

$people=array('name','sex','nation','brith');

 代码如下 复制代码


$people=array('name','sex','nation','birth');
echo $people[2];
?>

 

As for how to display the value of each element in the array, we use the index starting from 0. The index number is in square brackets after the variable name, such as

The code is as follows Copy code

$people=array('name','sex','nation','birth');

代码如下 复制代码


$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth');
echo $peoples['cs'];
?>

 

echo $people[2];

?>

The output $people[2] shows nation (the first item of the index is counted from 0).

In addition to supporting numerical index arrays, PHP also supports related arrays. The so-called related array means that you can customize keywords to replace unintuitive

 代码如下 复制代码


$people[0]='name';
$people[1]='sex';
$people[2]='nation';
$people[3]='brith';

Numeric index, such as:

The code is as follows Copy code
 代码如下 复制代码


$peoples['xm']='name';
$peoples['xb']='sex';
$peoples['mz']='nation';
$peoples['cs']='birth';

$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth'); echo $peoples['cs']; ?>
Using related arrays makes the selection of output intuitive (no need to pre-compute index numbers and then output), and use between defined keywords and values “=>” symbol definition. According to the two display methods of PHP array elements, numbers can be automatically created directly without the need for array() declaration and initialization like variables. For example
The code is as follows Copy code
$people[0]='name'; $people[1]='sex'; $people[2]='nation'; $people[3]='brith';
or
The code is as follows Copy code
$peoples['xm']='name'; $peoples['xb']='sex'; $peoples['mz']='nation'; $peoples['cs']='birth';

The size of the array changes dynamically according to the number of elements added.

The code is as follows
 代码如下 复制代码


//索引数组
    $user[0]=1;//用户序号
    $user[1]="zhangsan";//用户名
    $user[2]=10;//年龄
    $user[3]="nan";//性别
    echo '

'; <br>
    print_r($user); <br>
    echo '
';
 
    //关联数组
    $user["id"]=1;
    $user["name"]="zhangsan";
    $user["age"]=10;
    $user["sex"];
    $user["age"]=90;//赋值
    echo $user["name"];//输出
 
    //使用array()声明数组
    $user=array(1,"zhangsan",10,"nan");
    //使用array()声明关联数组
    $user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
 
    //声明多维数组(多条记录),来保存一个表中的多条用户信息记录
    $user=array(
        //用$user[0]调用这一行,比如调用这条记录中的姓名,$user[0][1]
        array(1,"zhangsan",10,"nan"),
        //用$user[1]调用这一行,比如调用这条记录中的姓名,$user[1][1]
        array(2,"lisi",20,"nv")
    );
 
    //数组保存多个表,每个表有多条记录
    $info=array(
        "user"=>array(
            array(1,"zhangsan",10,"nan"),
            array(2,"lisi",20,"nv")
        ),
 
        "score"=>array(
            array(1,90,80,70),
            array(2,60,40,70)
        )
 
 
 
    );
 
    echo $info["score"][1][1];//输出60,

Copy code

//index array
$user[0]=1;//User serial number
$user[1]="zhangsan";//Username
$user[2]=10;//Age
$user[3]="nan";//Gender
echo '
'; <br>
Print_r($user); <br>
echo '
';

//Associative array
$user["id"]=1;
$user["name"]="zhangsan";
$user["age"]=10;
$user["sex"];
$user["age"]=90;//Assignment
Echo $user["name"];//Output

//Use array() to declare an array
$user=array(1,"zhangsan",10,"nan");
//Use array() to declare an associative array
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");

//Declare a multi-dimensional array (multiple records) to save multiple user information records in a table
$user=array(
//Use $user[0] to call this line, such as calling the name in this record, $user[0][1]
array(1,"zhangsan",10,"nan"),
//Use $user[1] to call this line, such as calling the name in this record, $user[1][1]
array(2,"lisi",20,"nv")
);

//Array saves multiple tables, each table has multiple records
$info=array(
"user"=>array(
array(1,"zhangsan",10,"nan"),
array(2,"lisi",20,"nv")
),

"score"=>array(
array(1,90,80,70),
array(2,60,40,70)
)



);

Echo $info["score"][1][1];//Output 60,

http://www.bkjia.com/PHPjc/628676.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/628676.html
TechArticle
There are several ways to define arrays in php, such as array(), or arr[] to implement array definition, as follows Let me introduce to my friends in detail various techniques about PHP array definition. Detailed explanation of PHP array is an important...
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