简体   繁体   English

$ var [] = array()和$ var = array()之间有什么区别?

[英]What's the difference between $var[] = array() and $var = array()?

When I wrote $var = array('index' => 'some value') , it showed me an error when displaying a form on a page on the browser: 当我写$var = array('index' => 'some value') ,它在浏览器页面上显示表单时显示错误:

Notice: Undefined offset: 0 in C:\\xampp\\htdocs\\learn\\php\\admin\\authors\\form.html.php on line 28.

But when I wrote it like this $var[] = array('index' => 'some value') , it showed the page perfectly. 但是当我像这样写$var[] = array('index' => 'some value') ,它完美地显示了页面。 So I have to put [] after variable name. 所以我必须在变量名后加上[]。 In my knowledge, I can create an array variable like this $var = array(some array) . 据我所知,我可以创建一个像$var = array(some array)这样的数组变量。

So actually what is the difference between those two? 那么实际上这两者有什么区别?

When you wrote $var = array('index' => 'some value'), you can't access $var[0], cause this index doesn't exist. 当你写$ var = array('index'=>'some value')时,你无法访问$ var [0],导致这个索引不存在。 You have to use $var['index'] to access the value. 您必须使用$ var ['index']来访问该值。

Using $var[] = array(..), you push array() into $var, so it's create a new entry with index 0. If you use var_dump($var), you will have : 使用$ var [] = array(..),将array()推送到$ var,这样它就会创建一个索引为0的新条目。如果使用var_dump($ var),您将拥有:

array([0] => array(['index'] => 'somevalue'));

The results are different: 结果不同:

$var = array('index' => 'some value');
var_dump($var);

// array(1) {
//   ["index"]=>
//   string(10) "some value"
// }

$var[] = array('index' => 'some value');
var_dump($var);

// array(1) {
//   [0]=>
//   array(1) {
//     ["index"]=>
//     string(10) "some value"
//   }
// }

If you look closely, the first example creates an associative array with one key pair. 如果仔细观察,第一个示例将创建一个具有一个密钥对的关联数组。 The second example creates an array that contains one item at index 0; 第二个示例创建一个数组,其中包含索引0处的一个项目; that one item being the associative array. 那一项是关联数组。

If you do.. $var[]=array(); 如果你这样做.. $var[]=array();

An empty array is stored as the first index inside the $var array.. 空数组存储为$var数组中的第一个索引。

Array
(
    [0] => Array
        (
        )

)

If you do.. $var = array(); 如果你这样做.. $var = array();

You are just declaring it $var as an array. 您只是将$var声明为数组。

Array
(
)

So obviously, you know what the difference is... 显然,你知道区别是什么......

A simple demo 一个简单的演示

$var[] = array() is a two dimensional array its like saying. $var[] = array()是一个二维数组,就像它说的那样。

 $var = array(); // 1 dimensional array
 $var[0] = array();

try using var_export($var) in your two different examples to see the difference. 尝试在两个不同的示例中使用var_export($var)来查看差异。

$var = array('index' => 'some value') means a variable with an array value inside $var = array('index' => 'some value')表示内部包含数组值的变量

$var[] = array('index' => 'some value') means an array with another array in the last index. $var[] = array('index' => 'some value')表示在最后一个索引中包含另一个数组的数组。 This is the same of write $var = array(array('index' => 'some value')) 这与写$var = array(array('index' => 'some value'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM