我只想在不同的级别从同一个数组引用数组的值。这是否可能,因为在我声明数组之后,我可以正确地引用该元素,但我可以在数组中这样做吗?谢谢!
$literature = [
'authors'=>[
['name' => 'hhh', 'adress' => 'hemingway@oldtimers.com','yearOfBirth' => '1869'],
['name' => 'yyy', 'adress' => 'saintexupéry@oldtimers.com','yearOfBirth' => '1900'],
['name' => 'zzz', 'adress' => 'conandoyle@oldtimers.com','yearOfBirth' => '1859']
],
'books'=>[
['title' => 'ggg', 'author' => $literature ['authors'][0]['name'], 'year' => 1943],
['title' => 'uuu', 'author' => $literature ['authors'][0]['name'], 'year' => 1887],
['title' => 'ttt!', 'author' => $literature ['authors'][0]['name'], 'year' => 1929],
['title' => 'vvv', 'author' => $literature ['authors'][0]['name'], 'year' => 1936],
['title' => 'ooo', 'author' => $literature ['authors'][0]['name'], 'year' => 1938]
]
];
echo $literature ['authors'][0]['name'];// thats a proper reference, that results in showing the value, but when i print the whole array, that value displays as zero
foreach ($literature as $innerKeylevel1 => $innerDatalevel1) {
foreach ($innerDatalevel1 as $innerKeylevel2 => $innerDatalevel2) {
foreach ($innerDatalevel2 as $dataKey => $data) {
echo $data . " - ";
}
echo "</br>";
}
}发布于 2019-07-09 13:07:23
不能在声明数组的同一部分引用数组--因为表达式是从右到左的(因此它创建数组,在数组中执行所有操作,并将其填充到值中,然后将其赋值给$literature变量),这意味着在声明和创建数组之前,PHP不知道它是什么--在定义数组之前,您可以不使用它。见PHP:操作符优先。从关于=赋值操作符的手册上看,
Associativity | Operators | Additional Information
--------------+---------------------------------------------+-------------------------
right | = += -= *= **= /= .= %= &= |= ^= <<= >>= | assignment相反,你可以多次声明,首先是作者,然后是书。
$literature = [];
$literature['authors'] = [
['name' => 'hhh', 'adress' => 'hemingway@oldtimers.com','yearOfBirth' => '1869'],
['name' => 'yyy', 'adress' => 'saintexupéry@oldtimers.com','yearOfBirth' => '1900'],
['name' => 'zzz', 'adress' => 'conandoyle@oldtimers.com','yearOfBirth' => '1859']
];
$literature['books'] = [
['title' => 'ggg', 'author' => $literature['authors'][0]['name'], 'year' => 1943],
['title' => 'uuu', 'author' => $literature['authors'][0]['name'], 'year' => 1887],
['title' => 'ttt!', 'author' => $literature['authors'][0]['name'], 'year' => 1929],
['title' => 'vvv', 'author' => $literature['authors'][0]['name'], 'year' => 1936],
['title' => 'ooo', 'author' => $literature['authors'][0]['name'], 'year' => 1938]
];
print_r($literature);发布于 2019-07-09 13:07:03
我从两个数组开始,然后将它们组合起来。
$authors = [
['name' => 'hhh', 'adress' => 'hemingway@oldtimers.com','yearOfBirth' => '1869'],
['name' => 'yyy', 'adress' => 'saintexupéry@oldtimers.com','yearOfBirth' => '1900'],
['name' => 'zzz', 'adress' => 'conandoyle@oldtimers.com','yearOfBirth' => '1859'],
];
$books = [
['title' => 'ggg', 'author' => $authors ['authors'][0]['name'], 'year' => 1943],
['title' => 'uuu', 'author' => $authors ['authors'][0]['name'], 'year' => 1887],
['title' => 'ttt!', 'author' => $authors ['authors'][0]['name'], 'year' => 1929],
['title' => 'vvv', 'author' => $authors ['authors'][0]['name'], 'year' => 1936],
['title' => 'ooo', 'author' => $authors ['authors'][0]['name'], 'year' => 1938]
];最后:
$literature = [
'authors'=> $authors,
'books' => $books,
];https://stackoverflow.com/questions/56953008
复制相似问题