如何在同一数组中引用数组元素



我想仅在另一个级别上引用来自同一数组的数组的值。这是可能的,因为在声明数组之后,我可以正确地指出该元素,但是我可以在数组内部执行此操作吗?谢谢!

$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>";
    }
}

您无法与声明的同一部分中引用数组 - 因为表达式是从右到左的(因此,它创建数组值,然后然后将其分配到$literature变量(,这意味着PHP在声明和创建数组之前不知道$literature是什么 - 因此,您可以在定义之后才使用它。请参阅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);

  • https://3v4l.org/jntua
  • 实时演示

我将从两个数组开始,然后组合它们。

$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,
];

最新更新