Thread: array keys

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

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    is it possible to use only a key in an
    array
    meaning:

    $arr["a"]="yo";
    $arr["bc"]="ygo";
    $arr["eea"]="wgyo";
    $arr["fa"];
    $arr["a"]="yo";

    and then somehow get all the keys without
    and value

    i tried:

    while (list ($key) = each ($arr)) {
    print $key;
    }

    but i doens't seem to work

    l8er and thnx

    ------------------
    Greetings lewi
  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    18
    each returns an array so you can't assign it to just one variable with a list.

    while (list ($key,$garbage) = each ($arr)) {
    print $key;
    }

    I often use the variable $garbage when I have to have data that I don't need assigned to a variable.
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    already tried that..

    $arr["newkey"]; //no value bind to key
    does this really work ?

    i use it in:
    if($arr["newkey"]) ..like an exists
    this works

    but printing all the elements somehow doesn't seem to work..

    can u give me an working example?

    ------------------
    Greetings lewi

    [This message has been edited by Lewi (edited October 27, 2000).]
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Melbourne, Victoria, Australia
    Posts
    104
    Rep Power
    14
    I can't imagine that $arr['keyname']; will work, as it's not a complete statement... ie you're not using an operator or function or expression on it.

    Try $arr['keyname'] = "";

    Not sure if it will initialise the array element, but give it a go.

    Josh
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    15
    The best way to check to see if a variable (or array) is set:

    if(isset($variable)){blah;}
    or
    if(!isset($variable)){blah;} //for NOT set

    best way to see if it is empty:
    if(empty($variable)){blah;}
    or
    if(!empty($variable)){blah;}
    if it is NOT empty.
  10. #6
  11. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    my mistake..

    was thinking c alike way

    thnx anyway

    ------------------
    Greetings lewi

Similar Threads

  1. sort: find duplicate string elements in array
    By sciencelife in forum Java Help
    Replies: 3
    Last Post: January 27th, 2004, 01:29 PM
  2. C++ programming help.
    By Programming1 in forum C Programming
    Replies: 1
    Last Post: January 20th, 2004, 10:47 AM
  3. Mismatched array keys.
    By fungku in forum PHP Development
    Replies: 3
    Last Post: January 10th, 2004, 02:47 AM
  4. another remove array values from an array
    By robee in forum PHP Development
    Replies: 6
    Last Post: November 20th, 2003, 12:09 AM
  5. multi dimensional arrays - Can't grasp how to?
    By Kaleina in forum PHP Development
    Replies: 17
    Last Post: November 5th, 2003, 03:18 PM

IMN logo majestic logo threadwatch logo seochat tools logo