PHP数组-括号 [英] PHP Array - brackets

查看:49
本文介绍了PHP数组-括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建PHP数组时是否有人知道[]的含义,以及是否确实需要。因为从我的角度来看。两种方法都足够了。

does someone know the meaning of the [ ] in the creation of a PHP array, and if it's really needed. Becasuse from my point of view. Both ways are sufficinent

方法1,带括号:

$cars[] = array ('expensive' => $BMW,
                 'medium'    => $Volvo,
                 'cheap'     => $Lada);

方式2,不带括号:

$cars   = array ('expensive' => $BMW,
                 'medium'    => $Volvo,
                 'cheap'     => $Lada);


推荐答案

阅读 http://php.net/manual/en/function.array-push.php 差异。

主要区别:


  • 引用 array() 仅在创建变量时才能完成。

  • $ var []可以将值添加到已经存在的数组中。

  • $ var []将创建一个数组,并且实际上与 $ var 相同的函数调用 = array(...);

  • citing array() can only be done when creating the variable.
  • $var[] can add values to arrays that already exist.
  • $var[] will create an array and is in effect the same function call as $var = array(...);


  • 您不能使用 $ var [] 方法在单个声明中分配多个值(请参见下面的示例4, )

  • You can not assign multiple values in a single declaration using the $var[] approach (see example 4, below).

一些工作过程:

1) 使用 array()

1) Using array()

$cars = "cats";
$cars = array("cars","horses","trees");

print_r($cars) ;

会输出

$cars --> [0] = cars
          [1] = horses
          [2] = trees

2)附加值

然后写 $ cars = array( goats); 不会附加值,而是初始化一个新数组,给出:

Then writing $cars = array("goats"); will NOT append the value but will instead initialise a NEW ARRAY, giving:

 $cars --> [0] = goats

但是如果使用方括号附加,则写 $ cars [] =山羊 会给您:

But if you use the square brackets to append then writing $cars[] = "goats" will give you :

 $cars --> [0] = cars
           [1] = horses
           [2] = trees
           [3] = goats 

您的原始问题意味着 = 符号右侧的任何内容将附加到当前数组(如果左侧)手侧的语法为 $ var [] ,但这将附加为数字。如上图所示。

Your original question means that whatever is on the right hand side of the = sign will be appended to current array, if the left hand side has the syntax $var[] but this will be appended Numerically. As shown above.

您可以通过填写键值来按键名附加事物: $ cars ['cheap'] = $ Lada;

You can append things by key name by filling in the key value: $cars['cheap'] = $Lada; .

您的示例1设置了一个数组保存在数组中,因此要访问值$ Lada,您应引用 $ cars [0] ['cheap'] 。示例2设置了数组,但是将覆盖任何现有的数组或变量中的值。

Your example 1 is setting that an array is held within an array, so to access the value $Lada you would reference $cars[0]['cheap'] . Example 2 sets up the array but will overwrite any existing array or value in the variable.

3)字符串和数字索引

使用 array(...)语法非常适合在数组创建时定义多个值时,这些值具有非数字或数字非线性键,例如您的示例:

The method of using the array(...) syntax is good for defining multiple values at array creation when these values have non-numeric or numerically non-linear keys, such as your example:

$cars = array ('expensive' => "BMW",
             'medium'    => "Volvo",
             'cheap'     => "Lada");

将以以下数组输出:

   $cars --> ['expensive'] = BMW
             ['medium'] = Volvo
             ['cheap'] = Lada

但是,如果您使用以下语法:

But if you used the alternative syntax of:

$cars[] = "BMW";
$cars[] = "Volvo";
$cars[] = "Lada";

这将输出:

$cars --> [0] = BMW
          [1] = Volvo
          [2] = Lada

4)我还在写......

再举一个例子:您可以结合使用<$ c $的效果c> array()和 $ var [] ,并在方括号内包含键声明:

As another example: You can combine the effects of using array() and $var[] with key declarations within the square brackets thus:

$cars['expensive'] = "BMW";
$cars['medium'] = "Volvo";
$cars['budget'] = "Lada";

给予:

   $cars --> ['expensive'] = BMW
             ['medium'] = Volvo
             ['cheap'] = Lada

(我的原始答案很冗长,但不是很好)。

(my original answer was verbose and not very great).

5)最后.....

那么当您结合这两种样式,将 array()声明与 $ var [] 附加项混合在一起:

So what happens when you combine the two styles, mixing the array() declaration with the $var[] additions:

$ray = array("goats" => "horny", "knickers" => "on the floor", "condition" => "sour cream");
$ray[] = "crumpet";
$ray[] = "bread";

这将同时维护数组中的数字和字符串键索引,并以输出print_r()

This will maintain both numeric and string key indexes in the array, outputting with print_r():

$ray --> [goats] => horny
         [knickers] => on the floor
         [condition] => sour cream
         [0] => crumpet
         [1] => bread

这篇关于PHP数组-括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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