Demonic Posted June 11, 2008 Share Posted June 11, 2008 Is it possible to check the type of array? I'm trying to see if its possible to check if an array is a associative array. Is that possible? I was thinking of something along the lines as: <?php $array = ('test','test'); function is_associative(array $array) { $getlength = sizeof($array); if($getlength <= 0 ) { return false; } for($x=0;$x<$getlength;$x++) { if(key($array[$x]) == $x) { if( $x > 0 ) { if( key($array[$x]) == intval(key($array[$x-1])) + 1 ) { } } else { continue; } } else { return false; } } } ?> Then I got a headache Quote Link to comment Share on other sites More sharing options...
maexus Posted June 11, 2008 Share Posted June 11, 2008 I don't have any sort of pseudo code but you could use array_keys and check if they are all int? Sorry, I'm tired. Quote Link to comment Share on other sites More sharing options...
Demonic Posted June 11, 2008 Author Share Posted June 11, 2008 hmm: <?php function is_associative(array $array) { $length = sizeof($array); if( $length <= 0 ) { return false; } foreach( $array as $ray ) { if( is_int( key($ray) ) || is_string( key($ray) ) ) { if( is_array( $ray ) ) { return true; } else { return false; } } else { return false; } } } ?> Might work Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 11, 2008 Share Posted June 11, 2008 technically all arrays are associative, because a value has to have a key, whether it is assigned by you or PHP. but, you could do something as maexus said.. $myARR1 = array(1 => 'dog',2 => 'cat',3 => 'pig'); $is_good = TRUE; foreach($myARR1 as $key => $val){ if(is_string($key)){ $is_good = FALSE; break; } } if($is_good){ echo 'This is a good array'; }else{ echo 'This is a bad array'; } Quote Link to comment Share on other sites More sharing options...
Demonic Posted June 11, 2008 Author Share Posted June 11, 2008 That wouldn't always work though as you can have arrays like: <?php $array = array(1 => 24, 24 => 54, 99 => '11'); ?> Or better yet: <?php $array = array(1 => array(1,2), 35 => 'x'); ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 11, 2008 Share Posted June 11, 2008 Then in my code, $is_good would return TRUE Quote Link to comment Share on other sites More sharing options...
Demonic Posted June 11, 2008 Author Share Posted June 11, 2008 Hmm wouldn't it always be TRUE? Quote Link to comment Share on other sites More sharing options...
btherl Posted June 11, 2008 Share Posted June 11, 2008 At the lowest level, arrays can be indexed by either an integer, a string or both. You can have both an integer and a string pointing to the same memory space as well (like a reference. Well it is a reference). For example, mysql_fetch_row() returns an array with both integer and string indexes pointing to the same memory. Actually you can point as many indexes of any type to the same memory space (subject to limits from integer overflow in reference counting). Demonic, if you want to check that all the keys are strings, then little guy's code reversed should work. Use is_int() instead of is_string(). What he wrote checks that all the keys are integers. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 11, 2008 Share Posted June 11, 2008 how bout this: <?php // sample data $array = array(1 => array('hollow' => 'pig',2), 'hollow' => 'x'); // comment string $comment = array(0 => '', 1 => 'NOT '); // legend: 1 false and 0 true // true means associative // false means not $type = 1; // true means multidimensional // false means not $multi = 1; // self explanable (am gettin lazy $is_array = 0; // do all the nasty stuffs function checkArrayType($data) { global $is_array; global $multi; global $type; // check if array if(!is_array($data)) { $is_array = 1; return false; } while($chunk = current($data)) { // check if an array inside an array if(is_array($chunk)) { $multi = 0; checkArrayType($chunk); } else { $index = key($data); // check if an array is associative if(is_string($index)) { $type = 0; } } next($data); } return true; } // start cookin' checkArrayType($array); // display text echo "<br><br><br>"; print_r($array); echo "<br><br>"; echo "This is " . $comment[$multi] . "a multidimensional array.<br>"; echo "This is " . $comment[$type] . "an associative array.<br>"; echo "This is " . $comment[$is_array] . "an array.<br>"; ?> hope this helps... God bless! cheers, Jay Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 11, 2008 Share Posted June 11, 2008 the idea here is to check the index rather than the value... to do this, you can use key(). using is_string() would do as long as the one being checked is the index rather than the value. Jay, Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.