note 102652 added to function.is-array

[email protected] Sat, 26 Feb 2011 09:12:36 -0800
Newsgroups php.notes
Message-ID <[email protected]>
If you wanted to have 'pure' associative arrays,
you could filter out non-string keys and then compare the count of the original array with the count of the filtered array.

<?php
	// Only validates empty or completely associative arrays
	function is_assoc ($arr) {
		return (is_array($arr) && count(array_filter(array_keys($arr),'is_string')) == count($arr));
	}

	$a = '';
	$b = 'z';
	$c = array();
	$d = array('x','y','z');
	$e = array('x','y','zed'=>'z');
	$f = array('ex'=>'x','why'=>'y','zed'=>'z');

	echo '$a: ' . ( (is_assoc($a)) ? 'true' : 'false' ) ."\n"; // False
	echo '$b: ' . ( (is_assoc($b)) ? 'true' : 'false' ) ."\n"; // False
	echo '$c: ' . ( (is_assoc($c)) ? 'true' : 'false' ) ."\n"; // True
	echo '$d: ' . ( (is_assoc($d)) ? 'true' : 'false' ) ."\n"; // False
	echo '$e: ' . ( (is_assoc($e)) ? 'true' : 'false' ) ."\n"; // False
	echo '$f: ' . ( (is_assoc($f)) ? 'true' : 'false' ) ."\n"; // True
?>	

I've tested the point of checking whether the array is empty first.
The overall result was a 200% speed increase when the array was indeed empty,
but an average 10% slow when it had elements inside.

<?php
	/***********************************************************
	 *	Benchmarks: checking for empty array vs. not checking  *
	 ***********************************************************/
	
	// With empty check
	function is_assoc ($arr) {
		return (is_array($arr) && (!count($arr) || count(array_filter(array_keys($arr),'is_string')) == count($arr)));
	}
	
	function test_speed($arr) {	
		$t = microtime(true);
		for($i = 0; $i < 100000; ++$i) is_assoc($arr);
		return (microtime(true) - $t);
	}
	
	echo '$a: ' . test_speed($a) ."\n"; // 0.20597505569458
	echo '$b: ' . test_speed($b) ."\n"; // 0.19199514389038
	echo '$c: ' . test_speed($c) ."\n"; // 0.28803396224976
	echo '$d: ' . test_speed($d) ."\n"; // 0.94685983657837
	echo '$e: ' . test_speed($e) ."\n"; // 0.96698403358459
	echo '$f: ' . test_speed($f) ."\n"; // 0.98052096366882
	
	// Without empty check
	function is_assoc_2 ($arr) {
		return (is_array($arr) && count(array_filter(array_keys($arr),'is_string')) == count($arr));
	}
	
	function test_speed_2($arr) {	
		$t = microtime(true);
		for($i = 0; $i < 100000; ++$i) is_assoc_2($arr);
		return (microtime(true) - $t);
	}
	
	echo '$a: ' . test_speed_2($a) ."\n"; // 0.18811202049255 = FASTER:  91.3% of is_assoc
	echo '$b: ' . test_speed_2($b) ."\n"; // 0.18782901763916 = FASTER:  97.8% of is_assoc
	echo '$c: ' . test_speed_2($c) ."\n"; // 0.64437484741211 = SLOWER: 223.7% of is_assoc
	echo '$d: ' . test_speed_2($d) ."\n"; // 0.84632205963135 = FASTER:  89.4% of is_assoc
	echo '$e: ' . test_speed_2($e) ."\n"; // 0.86739897727966 = FASTER:  89.7% of is_assoc
	echo '$f: ' . test_speed_2($f) ."\n"; // 0.87963700294495 = FASTER:  89.7% of is_assoc
?>
----
Server IP: 69.147.83.197
Probable Submitter: 91.177.149.48
----
Manual Page -- http://www.php.net/manual/en/function.is-array.php
Edit        -- https://master.php.net/note/edit/102652
Del: integrated  -- https://master.php.net/note/delete/102652/integrated
Del: useless     -- https://master.php.net/note/delete/102652/useless
Del: bad code    -- https://master.php.net/note/delete/102652/bad+code
Del: spam        -- https://master.php.net/note/delete/102652/spam
Del: non-english -- https://master.php.net/note/delete/102652/non-english
Del: in docs     -- https://master.php.net/note/delete/102652/in+docs
Del: other reasons-- https://master.php.net/note/delete/102652
Reject      -- https://master.php.net/note/reject/102652
Search      -- https://master.php.net/manage/user-notes.php