Strawberry
Strawberry

Reputation: 67868

What keys are generated when pushing an array element with empty square bracket syntax $array[]?

$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";

When declaring it this way, does it automatically increase the index? Why is this ideal?

Upvotes: 0

Views: 190

Answers (5)

Babiker
Babiker

Reputation: 18798

Yes its ideal, because if its not, then use an associative array.

Upvotes: 0

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95449

Yes, it does. See the PHP arrays guide. Note that if you want to delete the array, you can use:

unset($a);

Or if you want to make $a become an empty array, you can use:

$a = array();

Upvotes: 1

User123
User123

Reputation: 556

Yes, It automatically increasing a index but start with '0'.

Upvotes: 0

b. e. hollenbeck
b. e. hollenbeck

Reputation: 6573

Yes, it does. It may not be 'ideal' - but considering that you're setting an array equal to a single value, adding to the array is better than overwriting the entire array, which I suppose would be the other option for that statement.

Upvotes: 0

Dominic Rodger
Dominic Rodger

Reputation: 99751

Yes it does. It may not be ideal for you, but it sure is convenient for most people.

Upvotes: 5

Related Questions