1

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>";
    }
}
5
  • What is the output you're trying to achieve? Commented Jul 9, 2019 at 12:59
  • can't use, you can start use once it is defined Commented Jul 9, 2019 at 13:00
  • Thanks! Thats what i thought. I'm trying to add a the data from the same array on another level. Guess i gonna have to do that after it's declared. Commented Jul 9, 2019 at 13:02
  • You can declare the $litterature array in two rounds; one for authors, and one for books. Then you can use the first, already declared part, in the second declaration. Commented Jul 9, 2019 at 13:04
  • Oh thank you that's even better Commented Jul 9, 2019 at 13:07

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Thanks! That' how i wanted to do it in a first place

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.