PHP 语法 $var1[] = $var2 是什么意思? [英] What does PHP syntax $var1[] = $var2 mean?

查看:49
本文介绍了PHP 语法 $var1[] = $var2 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 语法 $var1[] = $var2 是什么意思?

What does PHP syntax $var1[] = $var2 mean?

注意 $var1 变量后面的 [].

Note the [] after $var1 varable.

推荐答案

这意味着 $var1 是一个数组,而这个语法意味着你将 $var2 插入到数组末尾的一个新元素.

It means that $var1 is an array, and this syntax means you are inserting $var2 into a new element at the end of the array.

所以如果你的数组之前有 2 个元素,就像这样:

So if your array had 2 elements before, like this:

$var1=( 1 => 3, 2 => 4)

而 $var2 的值为 5,它不会是这样的:

and the value of $var2 was 5, it would not look like this:

$var1=( 1 => 3, 2 => 4, 3 => 5)

这也意味着如果 $var2 本身就是一个数组,那么您刚刚创建了一个二维数组.假设如下:

It also means that if $var2 was an array itself, you have just created a two dimensional array. Assuming the following:

$var1=( 1 => 3, 2 => 4)
$var2=( 1 => 10, 2=>20)

执行 $var1[]=$var2; 会导致这样的数组:

doing a $var1[]=$var2; wouls result in an array like this:

$var1=( 1 => 3, 2 => 4, 3 => (1 =>10, 2 => 20))

顺便说一句,如果您还没有使用过多维数组,那么访问数据非常简单.如果我想使用新数组中的值 20,我可以这样做:

As an aside, if you haven't used multi-dimensional arrays yet, accessing the data is quite simple. If I wanted to use the value 20 from our new array, I could do it in this manner:

echo $var1[3][2];

注意第二组方括号 - 基本上你是说我想访问 $var1 数组的元素 3 内的数组的元素 2.

Note the second set of square brackets - basically you are saying I want to access element two of the array inside element 3 of the $var1 array.

关于这一点,有一点需要注意.如果您正在使用多维数组,则此语法可以让您在某种循环结构中脱颖而出.假设您有一个二维数组,您在其中存储了一些记录并希望从数据库中获取更多记录:

On that note, there is one thing to be aware of. If you are working with multi-dimensional arrays, this syntax can catch you out inside a loop structure of some sort. Lets say you have a two dimensional array where you store some records and want to get more from the database:

$var1 = ( 
    1 => (id =>1, age => 25, posts => 40),
    2 => (id =>2, age => 29, posts => 140),
    3 => (id =>3, age => 32, posts => 12)
    )

如下所示的循环:

while($row=someDatabaseRow)
{
    $var1[]=$row['id']; // value 4
    $var1[]=$row['age']; // value 21
    $var1[]=$row['posts']; // value 34
}

实际上会为每次执行插入一个新元素,因此您的数组最终将如下所示:

will infact insert a new element for every execution, hence your array would end up looking like this:

$var1 = ( 
    1 => (id =>1, age => 25, posts => 40),
    2 => (id =>2, age => 29, posts => 140),
    3 => (id =>3, age => 32, posts => 12),
    4 => 4,
    5 => 21,
    6 => 34
    )

正确的方法是先组装数组,然后将其附加到当前数组以维护结构,如下所示:

The correct way would be to assemble the array first, then append it to your current array to maintain the strucutre, like this:

while($row=someDatabaseRow)
{
    $tempArr= array();
    $tempArr[]=$row['id']; // value 4
    $tempArr[]=$row['age']; // value 21
    $tempArr[]=$row['posts']; // value 34
    $var1[]=$tempArr;
}

现在你的数组看起来像你预期的那样,即:

Now your array would look like you expected, namely:

$var1 = ( 
    1 => (id =>1, age => 25, posts => 40),
    2 => (id =>2, age => 29, posts => 140),
    3 => (id =>3, age => 32, posts => 12)
    4 => (id =>4, age => 21, posts => 34)
    )

这篇关于PHP 语法 $var1[] = $var2 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆