数组类型变量传值_Php –数组变量类型

数组类型变量传值

数组类型变量传值

[rps-include post=6522]

[rps-include post = 6522]

We have used single value variable types up to now. But the real world problems may need different solutions and variables then those. We generally define,use multiple and a lot of values. Defining one by one variable is not a practical solution for these situations.

到目前为止,我们已经使用了单值变量类型。 但是现实世界中的问题可能需要与之不同的解决方案和变量。 我们通常定义,使用多个值。 对于这些情况,一一定义变量不是可行的解决方案。

For example we want to store 5 students names in a single variable. We call these as arrays. Arrays can hold multiple values.

例如,我们要在一个变量中存储5个学生姓名。 我们称这些为数组。 数组可以容纳多个值。

定义数组 (Define Array)

Arrays can be defined in different ways. One of them is just providing the type as array. This will not put any data into array. In this example we will create a array named $students .

数组可以用不同的方式定义。 其中之一只是将类型提供为数组。 这不会将任何数据放入数组。 在此示例中,我们将创建一个名为$students的数组。

$students=array();

We can also define an array just initializing or giving values like below.

我们还可以定义一个仅初始化或给出如下值的数组。

初始化数组 (Initialize Array)

We programmers generally use most faster way of defining and initializing an array. We will simply provide the array elements in square brackets by separating them with command. In this example we will define array named $students and put students names elements.

我们程序员通常使用最快的方法来定义和初始化数组。 通过使用命令将它们分开,我们将简单地在方括号中提供数组元素。 在此示例中,我们将定义名为$students数组,并放置学生的名称元素。

$students = array("Ahmet" , "Ali", "İsmail" , "Elif"  );

This type of array definition and initialization is same as like below.

这种类型的数组定义和初始化与以下相同。

$students = array();

$students[0] = "Ahmet";

$students[1] = "Ali";

$students[2] = "İsmail";

$students[3] = "Elif";

索引编号 (Index Numbers)

As you see we are using numbers to specify array index. Index is used to specifically specify an element in array. Index numbers starts from  by default. This means default array with 4 element index numbers starts from  and up to 3 . We can access array elements by giving index number. Index numbers are specified in square brackets.

如您所见,我们使用数字来指定数组索引。 索引用于专门指定数组中的元素。 索引号从默认情况下。 这意味着具有4个元素索引号的默认数组从最多3 。 我们可以通过给出索引号来访问数组元素。 索引号在方括号中指定。

In the following example we will change the element which index numbers is 1 .

在下面的示例中,我们将更改索引号为1的元素。

$students[1] = "Jack";

关联数组 (Associative Arrays)

We have used index numbers to specify specific elements in previous part. Array have another usage for specifying elements in array. It is called associative . We can set strings to specify index numbers like names. In this example we will use students names as array element specifier.

我们已经使用索引号来指定上一部分中的特定元素。 数组还有另一种用法,用于指定数组中的元素。 它被称为associative 。 我们可以设置字符串以指定诸如名称之类的索引号。 在此示例中,我们将使用学生姓名作为数组元素说明符。

$students=array("ismail"=>33,"ali"=>5,"elif"=>9);

And we can access associative arrays by specifying string. We will get ismail age from associative array.

并且我们可以通过指定字符串来访问关联数组。 我们将从关联数组中获取ismail年龄。

$age = $students["ismail"];

多维数组 (Multidimensional Arrays)

We have use single dimension arrays up to now. We can also use array inside array. These type of arrays called multidimensional arrays. There is no restriction about the level of dimensions. In the following example we will define an array which holds student information arrays inside it.

到目前为止,我们一直在使用一维数组。 我们也可以在array内部使用array。 这些类型的数组称为多维数组。 尺寸级别没有限制。 在下面的示例中,我们将定义一个数组,其中包含学生信息数组。

$students=array( array("ismail",12), array("ali",5) , array("elif",9) );

We can access the inner arrays elements and values by using square brackets.

我们可以使用方括号访问内部数组的元素和值。

echo $students[0][0];

echo $students[0][1];

打印阵列 (Print Arrays)

Printing whole array is one of the most used function and debug operation while developing applications. We can print whole array with the print_r() function by providing the array. We will print array named $student in the following example.

在开发应用程序时,打印整个阵列是最常用的功能和调试操作之一。 通过提供数组,我们可以使用print_r()函数打印整个数组。 在下面的示例中,我们将打印名为$student数组。

print_r($students);
Print Arrays
Print Arrays
打印阵列

[rps-include post=6522]

[rps-include post = 6522]

LEARN MORE  Java ArrayList Tutorial with Examples
了解更多带有示例的Java ArrayList教程

翻译自: https://www.poftut.com/php-array-variable-type/

数组类型变量传值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值