Solved

PHP array offsets

Posted on 2011-02-28
5
757 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:handypam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 50 total points
ID: 35002613
Sure - just declare a null value for the first element of the array.

$fruit[] = NULL;
$fruit[]="firstfruit";
$fruit[]="secondfruit";
0
 
LVL 14

Expert Comment

by:R-Byter
ID: 35002622
There is no hidden setting for that. All you need to do is to access array elements differently. May I ask you to write the code where you would use it?

Regards
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35004439
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.
0
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 35006446
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?
0
 

Author Comment

by:handypam
ID: 35006635
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question