#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    139
    Rep Power
    10

    A quick way to initialize an associative array.


    Is there a quick way to initialize all of the potential keys in an
    associative array
    so as to avoid the "illegal offset" notice that you get when referencing an index that ended up not getting set?

    For example:

    $stuff = array();

    if($foo == 1){
    $stuff['bar'] = 'high';
    }

    //this produces a notice when foo doesn't equal one.
    echo "bar is ".$stuff['bar'];

    Obviously in the example it wouldn't be much just to initialize "
    $stuff['bar'] = ''; ", but it's a pain when an array could have or not
    have many indexes.
    Thanks,
    Dan

    Operating system ubuntu 12.04
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Um ... what? How about simply writing correct code? If you check the existence of a value before you use it, you'll never into the "undefined offset" warning again.

    Enumerating "all of the potential keys" means enumerating every possible PHP string. That's more than this planet can hold.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Location
    US
    Posts
    103
    Rep Power
    54
    Don't get scared just because the variable names are contained in something different.
    No different than an operating system expecting a filename to exist inside a directory that you created. Or you expecting the script you created and named inside that directory to contain the variable you defined inside that script. Arrays just have the benefit of iteration and a different storage mechanism via pointers.

    Just to add,
    you can use code like this:
    PHP Code:
    if(isset($arr['assocKey'])){
    //do something;
    }

    //this will NOT work because a notice is not an exception!
    try{
    $msg 'i have a ' $arr['assocKey'] . ' in my array.';
    }catch{
    $arr['assocKey'] = 'value';

    or you might try using an array function like array_filter and pass as a reference to initialize all keys with values so you don't forget to.
    Last edited by nightFix; November 2nd, 2013 at 08:38 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    139
    Rep Power
    10
    Originally Posted by nightFix
    Don't get scared just because the variable names are contained in something different.
    No different than an operating system expecting a filename to exist inside a directory that you created. Or you expecting the script you created and named inside that directory to contain the variable you defined inside that script. Arrays just have the benefit of iteration and a different storage mechanism via pointers.

    Just to add,
    you can use code like this:
    PHP Code:
    if(isset($arr['assocKey'])){
    //do something;
    }

    //this will NOT work because a notice is not an exception!
    try{
    $msg 'i have a ' $arr['assocKey'] . ' in my array.';
    }catch{
    $arr['assocKey'] = 'value';

    or you might try using an array function like array_filter and pass as a reference to initialize all keys with values so you don't forget to.

    Thanks Nightfix
    Thanks,
    Dan

    Operating system ubuntu 12.04

IMN logo majestic logo threadwatch logo seochat tools logo