Click to See Complete Forum and Search --> : Array initialization


rm_vk
05-26-2005, 09:29 AM
If I add a Element to an array, it gets added at position 0. But is it possible to add element to array, beginning at position 1.

That is instead of dum[0] can we have dum[1].

jinkas
05-26-2005, 09:42 AM
try this:

$dum[1]='string';

NogDog
05-26-2005, 09:48 AM
If you explicitly set the first index to 1, the rest will increment from there:

$myArray = array(1 => "Value 1");
$myArray[] = "Value 2";
$myArray[] = "Value 3";
print_r($myArray);

# or

$myArray[1] = "Value 1";
$myArray[] = "Value 2";
$myArray[] = "Value 3";
print_r($myArray);

Or, you could count() the elements already in the array and add 1:

for($ix=1; $ix<=3; $ix++)
{
$myArray[count($myArray)+1] = "Value $ix";
}
print_r($myArray);