Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    is_associative function?

    Hi,

    Ok, so I know this function doesn’t exist, but is there an equivalent? Something that will return true or false and tell me if an array is associative?

    Thanks,

    Jack

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Associative keys are string keys. This function will check if there is at least one string kay and return true if there is one.
    PHP Code:
    function is_associative($array)
    {
        if(!
    is_array($array))
        {
            return 
    false;
        }
        
    krsort($arraySORT_STRING);
        return !
    is_numeric(key($array));

    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool, cheers, i had just thrown this together, which works, but i should imagine yours is better as it dosent actually loop through the array, so i guess it would be faster?

    PHP Code:

    function is_associative($input) {

        
    $result false;
        
        foreach(
    $input as $key => $value) {
            if(
    is_string($key)) {
                
    $result true;
                break;
            }
        }
        
        return 
    $result;


    Thanks!


  •  

    Posting Permissions

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