I have an array and I want to check if the key equals to a specific string, so I can use that information and give a new value to another variable $x, depending on which key is given.
So far I tried if (array_key_exists('slot1', $logoImages2)) {}
(for example), but that does not seem to work.
So the array looks something like this:
'images2' => [
'slot1' => 'images/Beutel.svg',
'slot2' => 'images/Bund.svg',
'images/Container.svg',
'slot7' => 'images/DIY.svg',
'images/Flasche.svg',
'images/Sack.svg',
'slot4' => 'images/Eimer.svg',
],
As you can see, some of the array items have a key and some do not have a key. Now I want to check, when a key is given, if the key is equal to a given value, like 'slot7' for example. So in theory I want to achieve something like this (this is just to explain my end goal):
foreach ($logoImages2 as $slot => $logo) {
if (isset($slot)) {
if (/* $slot is equal to 'slot7' */) {
$x = $x2
} elseif (/* $slot is equal to 'slot9' */) {
$x = $x4
}
}
}
You can foreach array and compare keys with value:
foreach ($logoImages2 as $slot => $logo) {
if ($slot === "slot7") {
$x = $x2
} elseif ($slot === "slot9") {
$x = $x4
}
}