看下面的例子来理解这个想法:
$arr = array
(
0 => array (
$this => 3, // instead 0 => 3,
'name' => 'three',
),
1 => array (
$this => 5, // instead 0 => 5,
'name' => 'five',
),
2 => array (
$this => 7, // instead 0 => 7,
'name' => 'seven',
),
);
对于每个索引都有一个二维数组。而不是调用$arr[0][0],转而调用$arr[0]。这些都是与期望结果相呼应的想象。
echo $arr[0]; // printing 3
echo $arr[0]['name']; // printing 'three'
echo $arr[0][0]; // printing 3 (not relevant)
有这样的东西吗?
在PHP 5中,您可以扩展ArrayObject使其成为类似数组的对象。您可以覆盖它的一些行为
class SillyArray extends ArrayObject {
public function __construct($a)
{
foreach($a as $key => $value) {
if (is_array($value)) {
$a[$key] = new SillyArray($value);
}
}
parent::__construct($a);
}
public function __toString() {
$result = parent::offsetGet(0);
if (isset($result)) {
return (string)$result;
}
return "";
}
}
$a = new SillyArray(
array(0 => array(
0 => 3,
'name' => 'there'
))
);
echo $a[0];
echo $a[0][0];
echo $a[0]['name'];
你正在尝试访问多维数组的元素
<?php
$marks = array(
"m" => array
(
"physics" => 35,
"maths" => 30,
"chemistry" => 39
),
"q" => array
(
"physics" => 30,
"maths" => 32,
"chemistry" => 29
),
"z" => array
(
"physics" => 31,
"maths" => 22,
"chemistry" => 39
)
);
/* Accessing multi-dimensional array values */
echo "Marks for m in physics : " ;
echo $marks['m']['physics'] . "<br />";
echo "Marks for q in maths : ";
echo $marks['q']['maths'] . "<br />";
echo "Marks for z in chemistry : " ;
echo $marks['z']['chemistry'] . "<br />";
?>
这将产生
Marks for m in physics : 35
Marks for q in maths : 32
Marks for z in chemistry : 39
https://3v4l.org/bpL4i 现在你的问题是通过调用$arr[0]返回$arr[0][0]的值,就像@icecub说的那样,那么这可以通过
来完成reset()函数,用于将数组的内部指针移动到第一个元素。链接
实现https://3v4l.org/Ide2L