r/PHPhelp • • Apr 29 '25

Solved Undefined array key with defined key?

I've been beating my head against the wall trying to figure out what is wrong here. 'photo' is a key in my var $sel_page, so I can't see why this is not working. Help please. Thanks.

This is my code: background-image:url(<?php echo PHOTO\\\\\\_PATH . $sel\\_page\\\\\\\['photo'\\\\\\\]; ?>);

This is my variable : <?php print\\\\\\_r($sel\\\\\\_page);?>

( [0] => Array (

[id] => 21

[menu_name] => login

[position] => 20

[visible] => 1

[link] => 1

[content] => login_c.php

[preheader] => 1

[filepath] => login.php

[photo] => sezia.jpg

[prev] => index.php

[next] => index.php

[description] => admin area...<br /> log in

[title] => admin area...<br /> log in

[headline] => admin area...<br />log in ))

this is the result: <b>Warning</b>: Undefined array key "photo" in ...

edit: is $sel_page['photo'], not what is above

0 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/saintpetejackboy Apr 30 '25

Multidimensional arrays are my favorite.

Unfortunately, when I was younger, I used to write a lot of bad code where I would be perfectly expecting certain parts of a multidimensional arrays to not exist (and I would try to access them anyway, just in the logical procedure of checking if there was data).

Thankfully, null coalesce cleaned up what regular logic couldn't, and I don't make that mistake any more.

Multidimensional arrays are one of the first "a-ha!" moments I had as a programmer forever ago, and I have been riding that wave ever since.

1

u/BokuNoMaxi May 01 '25

Before PHP 8 it wasn't even a problem. It was simply ignored or created a warning that was simply ignored because of php.ini settings... Now you get a runtime exception 😁

Love my life as a backend dev that upgrades 20yrs old code from some other guys who did really wild stuff...