我只想从同一级别的同一数组中引用数组的值。甚至有可能,因为在声明数组之后,我可以正确地引用该元素,但是我可以在数组内部进行操作吗?谢谢!
$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>";
}
}
答案 0 :(得分:1)
我将从两个数组开始,然后将它们组合。
$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,
];
答案 1 :(得分:1)
您不能在声明数组的同一部分引用数组-因为表达式是从右到左的(所以它将创建数组,在数组中执行所有操作,并用值 then < / em>将其分配到$literature
变量中),这意味着PHP在声明并创建数组之前不知道$literature
是什么-为此,您必须在定义数组之后才能使用它。参见PHP: Operator Precedence。手册中关于=
赋值运算符
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);