Professional Web Applications Themes

Key() - PHP Development

According to the manual, key() returns the index element of the current array position. I have an array Array ( [7857] => Soaps [7861] => Sponges [7863] => Bath Brushes [7858] => Toothpastes [7865] => Colognes [7960] => Miscellaneous ) When I go $pline=key($plines); I get nothing for $pline. What is going on ? Is it possible that the array does not have a current array position ?...

  1. #1

    Default Key()

    According to the manual, key() returns the index element of the current
    array position.

    I have an array
    Array
    (
    [7857] => Soaps
    [7861] => Sponges
    [7863] => Bath Brushes
    [7858] => Toothpastes
    [7865] => Colognes
    [7960] => Miscellaneous
    )


    When I go
    $pline=key($plines);
    I get nothing for $pline.

    What is going on ?
    Is it possible that the array does not have a current array position ?
    meltedown Guest

  2. #2

    Default Re: Key()

    meltedown wrote: 

    What I'm trying to do is get the first index in the array.
    The only way is something like:
    foreach($plines as $p){
    $pline=key($plines);break;
    }

    Is there a better way ?
    meltedown Guest

  3. #3

    Default Re: Key()

    "meltedown" <org> kirjoitti
    viestissä:wlpJe.1011472$news.easynews.com... 

    where ever the internal pointer of the array points, there's a function
    called reset that - suprisingly resets the array pointer to the primary
    element of the array.

    Try the following example:

    <?php
    $fruits = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple',
    );

    reset($fruits);
    $key = key($fruits);
    $value = current($fruits);

    echo "$key = $value";
    ?>

    That should be sort of what you wanted.

    --
    SETI Home - Donate your cpu's idle time to science.
    Further reading at <http://setiweb.ssl.berkeley.edu/>
    Kimmo Laine <com>


    Kimmo Guest

  4. #4

    Default Re: Key()

    Kimmo Laine wrote: 
    >
    >
    > where ever the internal pointer of the array points, there's a function
    > called reset that - suprisingly resets the array pointer to the primary
    > element of the array.
    >
    > Try the following example:
    >
    > <?php
    > $fruits = array(
    > 'fruit1' => 'apple',
    > 'fruit2' => 'orange',
    > 'fruit3' => 'grape',
    > 'fruit4' => 'apple',
    > 'fruit5' => 'apple',
    > );
    >
    > reset($fruits);
    > $key = key($fruits);
    > $value = current($fruits);
    >
    > echo "$key = $value";
    > ?>
    >
    > That should be sort of what you wanted.
    >[/ref]
    Thanks, that works.
    meltedown Guest

  5. #5

    Default Re: Key()

    meltedown wrote:
     
    >
    > What I'm trying to do is get the first index in the array.
    > The only way is something like:
    > foreach($plines as $p){
    > $pline=key($plines);break;
    > }
    >
    > Is there a better way ?[/ref]

    $tab = array_keys($plines);
    $pline = $tab[0];


    otiteca Guest

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139