Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Hopefully simple question about arrays!

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #35421
    jacorre
    Participant

    Hi everyone,

    Hoping this is a simple question and I can explain it well.

    Let’s say I have the following array:


    $measurements = array (
    '',
    'Teaspoons',
    'Tablespoons',
    'Ounces'
    );

    It is my understanding that if you don’t define a key, then it uses numbers starting at 0.

    So:

    0 = blank
    1 = Teaspoons
    2 = Tablespoons
    3 = Ounces

    Let’s say I’m using the above array in a drop down field in WordPress. The information is being saved as a custom field.

    What if in the future I decide that I need to add more entries to the array. So let’s say I add the following:


    $measurements = array (
    '',
    'Teaspoons',
    'Boxes', // new entry added
    'Tablespoons',
    'Ounces'
    );

    Because I added the new entry between already existing entries, does that mean that the key numbers for the others have been thrown out of whack? Previously, Tablespoons was 2. Now it would be 3. And if so, then I’m assuming that anything I’ve filed previously will also be thrown out of whack correct?

    #91709
    SgtLegend
    Member

    Yes that is correct, it is better practice to append new array items to the end of the array to avoid such issues.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.