有人能用最简单的方式向我解释以下之间的区别吗?在阅读了PHP数组之后,我仍然不明白
print $myArray[0]->token
和
print $myArray[0]["token"]
编辑:
问题不在于最佳方法,而在于其意义。这里的答案可能很好,但它不是我的问题
这一切的一个例子:
<?php
//Creating simple object
$bookObject = new stdClass;
$bookObject->title = "Harry Potter and the Prisoner of Azkaban";
//Adding object to books array
$books = array($bookObject);//this array is equivalent to your $myArray
//Acessing object using -> operator
echo $books[0]->title;
//Re-initializng books array
$books = array(0=>array("title" => "Harry Potter and the Prisoner of Azkaban"));
//Accessing elements of an array by key
echo $books[0]['title'];
?>
$myArray
是具有对象的数组,属性(属性,变量)命名为token
作为其第一个元素。$myArray
是一个具有关联数组的数组,键命名为token
作为其第一个元素。
因此,这是关于两个不同的数据结构,数组作为第一个由0索引的元素。
- 具有
token
元素的数组内的对象 - 具有
token
元素的数组内的数组