explicit numeric keys vs item position

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nickdevx@hotmail.com

    explicit numeric keys vs item position

    Hi

    Say if I have a mixed array:

    $array = array("item1", "2"=>"item2 ", "5", "item4key"=>"it em4")

    Is it possible while looping through the array (foreach ($array as
    $key=>$val)) to check if an item has an explicit key specified instead
    of its automatically assigned position key?

  • Rik

    #2
    Re: explicit numeric keys vs item position

    nickdevx@hotmai l.com wrote:[color=blue]
    > Hi
    >
    > Say if I have a mixed array:
    >
    > $array = array("item1", "2"=>"item2 ", "5", "item4key"=>"it em4")
    >
    > Is it possible while looping through the array (foreach ($array as
    > $key=>$val)) to check if an item has an explicit key specified instead
    > of its automatically assigned position key?[/color]

    It seems like the explicit keys aren't numbers, in that case this will do:

    $explicit = (is_numeric($ke y)) ? false : true;

    If your explicit keys can be numbers I don't think it's possible, to my
    knowledge PHP doesn''t store information about how an elemtn is added in an
    array.

    Grtz,

    --
    Rik Wasmus


    Comment

    • Colin McKinnon

      #3
      Re: explicit numeric keys vs item position

      nickdevx@hotmai l.com wrote:
      [color=blue]
      > Hi
      >
      > Say if I have a mixed array:
      >
      > $array = array("item1", "2"=>"item2 ", "5", "item4key"=>"it em4")
      >
      > Is it possible while looping through the array (foreach ($array as
      > $key=>$val)) to check if an item has an explicit key specified instead
      > of its automatically assigned position key?[/color]

      Confused am I. An explicit key? An automatically assigned position key? If a
      key is not explicitly used in the assignment, then the next available
      numeric key is used. In other words by the time you are trying to work out
      what the keys are you've already passed a really bad bug in your code -
      either assign all your array values with PHP working out the indices, or
      specify all the keys yourself. Mixing the two together, particularly if you
      want to use numeric keys, is just asking for trouble.

      C.

      Comment

      Working...