Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-19-2006, 03:47 PM   PM User | #1
Jak-S
Regular Coder

 
Join Date: Mar 2005
Location: Brighton, UK
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
Jak-S is an unknown quantity at this point
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
Jak-S is offline   Reply With Quote
Old 02-19-2006, 04:12 PM   PM User | #2
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
marek_mar is offline   Reply With Quote
Old 02-19-2006, 04:23 PM   PM User | #3
Jak-S
Regular Coder

 
Join Date: Mar 2005
Location: Brighton, UK
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
Jak-S is an unknown quantity at this point
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!
Jak-S is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:56 PM.


Advertisement
Log in to turn off these ads.