Jump to content

How to: Check the Type of Array


Demonic

Recommended Posts

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  ;D

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.