I am trying to get a value from an array, but when I use $array[0], I get a PHP notice ‘Undefined Offset: 0’. However, when I use $array['id'] (should be the same as using ‘0’, since ‘id’ is the first key in the array), I am able to get the value. I did a print_r and this was outputted:
Array (
[id] => 1
[username] => test
)
Shouldn’t I be able to get the key using its index? I believe it was working earlier, but I don’t know what I could have done to make it stop working. Any ideas?
No, you shouldn’t be able to do this.
You might be thinking of arrays obtained from something like
mysql_fetch_array, which, by default, lets you use both numeric and string indexes to get to the columns – but the reason you can do that is because the array contains both of those.You can also use
array_valuesto extract values from an array to essentially transform the indexes to numeric ones, but it’s not the same thing, really.