php - How does a programmer deal with problems arising out of runtime additions of this kind to an array? -
the array problem asked can seen in code.
$sample=array('2'=>"a", "abc"=>"b", 2=>"c"); $sample_keys=array_keys($sample); foreach ($sample_keys $keys) { echo $keys, $sample[$keys],"\n"; } output 2c abcb $sample[0] gives error, $sample[1]
how programmer deal problems arising out of runtime additions of kind array? newbie in php
according manual: "strings containing valid integers cast integer type. e.g. key '8' stored under 8. on other hand '08' not cast, isn't valid decimal integer."
therefore, in code:
$sample_keys=array_keys($sample); the variable $sample_keys contains values 2 , abc.
as far comment "$sample[0] gives error, $sample[1]," of course do. array doesn't contain keys 0 , 1. contains 2 , abc.
the programmer can control type of thing @ runtime planning ahead possible values can added array. if necessary, can use functions such array_key_exists find out if given key in array , make adjustments necessary.
Comments
Post a Comment