Grow your CSS skills. Land your dream job.

Hopefully simple question about arrays!

  • # December 1, 2011 at 3:53 pm

    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?

    # December 1, 2011 at 8:34 pm

    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)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".