首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何引用同一数组中的数组元素?

如何引用同一数组中的数组元素?
EN

Stack Overflow用户
提问于 2019-07-09 12:49:42
回答 2查看 59关注 0票数 1

我只想在不同的级别从同一个数组引用数组的值。这是否可能,因为在我声明数组之后,我可以正确地引用该元素,但我可以在数组中这样做吗?谢谢!

代码语言:javascript
复制
$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>";
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-09 13:07:23

不能在声明数组的同一部分引用数组--因为表达式是从右到左的(因此它创建数组,在数组中执行所有操作,并将其填充到值中,然后将其赋值给$literature变量),这意味着在声明和创建数组之前,PHP不知道它是什么--在定义数组之前,您可以不使用它。见PHP:操作符优先。从关于=赋值操作符的手册上看,

代码语言:javascript
复制
Associativity |     Operators                               | Additional Information
--------------+---------------------------------------------+-------------------------
right         | = += -= *= **= /= .= %= &= |= ^= <<= >>=    | assignment

相反,你可以多次声明,首先是作者,然后是书。

代码语言:javascript
复制
$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);
票数 1
EN

Stack Overflow用户

发布于 2019-07-09 13:07:03

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

代码语言:javascript
复制
$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]
];

最后:

代码语言:javascript
复制
$literature = [
    'authors'=> $authors,
    'books' => $books,
];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56953008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档