php function to ensure array is serially indexed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    #1

    php function to ensure array is serially indexed

    hi,

    I need to know if there is a built in php function that can tell me whether an array is serially indexed or not.
    i.e. that all the keys range from 0 to ($count - 1)
    I could use a for loop, but I would prefer a built in function (internally written in C).
    so any one knows?

    thanks
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    I don't believe there is. Can you explain in what context you'd need this functionality?

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      if everything else fails, you still could use each/foreach

      Comment

      • guillermobytes
        New Member
        • Jan 2010
        • 77

        #4
        Originally posted by guillermobytes
        hi,

        I need to know if there is a built in php function that can tell me whether an array is serially indexed or not.
        i.e. that all the keys range from 0 to ($count - 1)
        I could use a for loop, but I would prefer a built in function (internally written in C).
        so any one knows?

        thanks
        Code:
        function isNumericallyIndexed($arr)
        {
            return array_keys($arr) === range(0, count($arr) - 1);
        }

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          there are classes, that make sure you create an "array" with only numerical indices: SplFixedArray & SplDoublyLinked List. though they’re nor suited to test an arbitrary array.

          Comment

          Working...