I have a key stored in a variable like so:
$key = 4;
I tried to get the relevant value like so:
$value = $array[$key];
but it failed. Help.
|
Your code seems to be fine, make sure that key you specify really exists in the array or such key has a value in your array eg:
Output:
Now:
Output:
|
|||||
|
|
|||
|
the simple way:
|
||||
|
As others stated, it's likely failing because the requested key doesn't exist in the array. I have a helper function here that takes the array, the suspected key, as well as a default return in the event the key does not exist.
hope it helps. |
|||
|
It should work the way you intended.
But maybe there is no element with the key
|
|||
|