curious array behavior
Posted: Tue Jun 18, 2013 11:56 am
In the following code snippet, an array is built based on the value of the variable 'days'. So the expected result for the given value "ace" should be
Array
(
[0] => 0
[1] => 2
[2] => 4
)
. . .however, if you try it, you will see that it ignores the first key->value pair unless value days is preceeded by an arbitrary character, such as the 'z' I added to the commented out line. I'm just trying to understand why I have to add that extra character for the array to populate correctly. Thoughts?
Array
(
[0] => 0
[1] => 2
[2] => 4
)
. . .however, if you try it, you will see that it ignores the first key->value pair unless value days is preceeded by an arbitrary character, such as the 'z' I added to the commented out line. I'm just trying to understand why I have to add that extra character for the array to populate correctly. Thoughts?
Code: Select all
$days = "ace";
if ($days) {
//$days = "z$days";
if (strpos($days, 'a')){
$weekset[]=0;}
if (strpos($days, 'b')){
$weekset[]=1;}
if (strpos($days, 'c')){
$weekset[]=2;}
if (strpos($days, 'd')){
$weekset[]=3;}
if (strpos($days, 'e')){
$weekset[]=4;}
if (strpos($days, 'f')){
$weekset[]=5;}
if (strpos($days, 'g')){
$weekset[]=6;}
echo "<pre>";
print_r($weekset);
echo "</pre>";