Alexei Taber
Alexei Taber

Reputation: 13

How can i reference an array element inside the same array?

I want to reference the value of an array from the same array only at a different level. Is that even possible, because after i declaring the array, i can refer to that element properly, but can i do that inside of an array? Thanks!

$literature = [
    'authors'=>[ 
        ['name' => 'hhh', 'adress' => '[email protected]','yearOfBirth' => '1869'],
        ['name' => 'yyy', 'adress' => 'saintexupé[email protected]','yearOfBirth' => '1900'],
        ['name' => 'zzz', 'adress' => '[email protected]','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>";
    }
}

Upvotes: 1

Views: 89

Answers (2)

Qirel
Qirel

Reputation: 26450

You can't reference the array in the same part as you declare it - as the expression is from right to left (so it creates the array, does everything in there and fills it with the values, then assigns it into the $literature variable), meaning that PHP doesn't know what $literature until the array has been declared and created - you can therefor not use it until after its been defined. See PHP: Operator Precedence. An exert from the manual about the = assignment operator,

Associativity |     Operators                               | Additional Information
--------------+---------------------------------------------+-------------------------
right         | = += -= *= **= /= .= %= &= |= ^= <<= >>=    | assignment

Instead you can declare it in multiple rounds, first the authors, then the books.

$literature = [];
$literature['authors'] = [ 
        ['name' => 'hhh', 'adress' => '[email protected]','yearOfBirth' => '1869'],
        ['name' => 'yyy', 'adress' => 'saintexupé[email protected]','yearOfBirth' => '1900'],
        ['name' => 'zzz', 'adress' => '[email protected]','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);

Upvotes: 1

delboy1978uk
delboy1978uk

Reputation: 12355

I would start with two arrays, then combine them.

$authors = [
    ['name' => 'hhh', 'adress' => '[email protected]','yearOfBirth' => '1869'],
    ['name' => 'yyy', 'adress' => 'saintexupé[email protected]','yearOfBirth' => '1900'],
    ['name' => 'zzz', 'adress' => '[email protected]','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]
];

And finally :

$literature = [
    'authors'=> $authors,
    'books' => $books,
];

Upvotes: 1

Related Questions