0

这是一个非常简单的问题,我正在努力解决。我有一个变量 ($foo),它是一个从 sql 调用创建的数组。

当我在代码中引用一个字段时,有什么区别

$foo['bar']$foo->bar

4

3 回答 3

1
$foo['bar']

references an element with the key 'bar' in an array assigned to the variable $foo, as retrieved by mysql_fetch_array()

$foo->bar 

references the property called 'bar' in an object instance in the variable $foo, as retrieved by mysql_fetch_object()

于 2012-06-12T10:24:36.483 回答
0

The difference is the mysqli_fetch_* method you use.

  • mysqli_fetch_object -> $foo->bar
  • mysqli_fetch_assoc -> $foo['bar']
  • mysqli_fetch_array -> $foo[0]

'Assoc' stands for 'associated (array)'.

于 2012-06-12T10:24:24.890 回答
0

$foo['bar'] is used to access array index this case $foo is an array variable and $foo->bar is used to access class method . in this case $foo is a class object

$foo = new StdClass();
$foo->bar = "Anything";

or

$foo['bar'] = "dfgdfG";
于 2012-06-12T10:24:43.597 回答