handypam
asked on
PHP array offsets
Hello experts,
is there such a thing in php (a global setting or something) to set the starting value of a generic php array to 1 instead of 0?
So for example.....
if i was to declare a new php array like
$fruit[]="firstfruit";
$fruit[]="secondfruit";
the first index would be $fruit[1] instead of $fruit[0]
thanks in advance
is there such a thing in php (a global setting or something) to set the starting value of a generic php array to 1 instead of 0?
So for example.....
if i was to declare a new php array like
$fruit[]="firstfruit";
$fruit[]="secondfruit";
the first index would be $fruit[1] instead of $fruit[0]
thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
By declaring you mean runtime assigning the elements to it ?
$fruit[1]="firstfruit";
$fruit[2]="secondfruit";
I think there's no point for that, like R-Byter wrote - just access them differently.
$fruit[1]="firstfruit";
$fruit[2]="secondfruit";
I think there's no point for that, like R-Byter wrote - just access them differently.
You can do what Ray suggested but I think you may add complexity elsewhere.
For example, if you iterate through the array using a foreach loop then you have to account for the fact that the first element will be defined and set to null.
My question is the same as everybody else's. Why would you want to do that?
For example, if you iterate through the array using a foreach loop then you have to account for the fact that the first element will be defined and set to null.
My question is the same as everybody else's. Why would you want to do that?
ASKER
the main reason is i am working with existing code that for some reason has an array offset of 1
i wanted my code to fit in and i couldnt be bothered to add the $array[0]=null or the orginalarray[1] = myarray[0+offset]
i was kinda hoping there was some secret global array offset setting that i didnt know about..
but thanks everyone
i wanted my code to fit in and i couldnt be bothered to add the $array[0]=null or the orginalarray[1] = myarray[0+offset]
i was kinda hoping there was some secret global array offset setting that i didnt know about..
but thanks everyone
Regards