I have the following code:
$id = $_GET['id'];
echo '<br>' . 'id: ' . $id . '<br><br>';
echo '<pre>';
print_r($itemQtys);
echo '</pre>';
echo '<br>' . 'itemqtys[id]: ' . $itemQtys[$id] . '<br>';
echo '<br>' . 'id: ' . $id . '<br>';
The output is:
id: 5
Array
(
[5] => 12
)itemqtys[id]:
id: 5
As you can see, when I try to access the value in the array using the $id variable as the key, no value is returned. However, when I do this:
echo '<br>' . 'itemqtys[5]: ' . $itemQtys[5] . '<br>';
The result is:
itemqtys[5]: 12
Why can't I use a variable to specify the index in the array?
$id? It's not set to anything in your example. How are you creating it?ini_set('display_errors', 1);at the start of your script (or read the error log) and you'll probably see a warning which will indicate that$idisn't exactly what you expect.