Re: Check for Associative Array

From: Date: Thu, 18 Jul 2002 18:28:08 +0000
Subject: Re: Check for Associative Array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-108312@lists.php.net to get a copy of this message
At 12:57 PM -0400 7/18/02, Henning Sittler wrote:
What's the best way to check for an Associative Array? There is no is_assoc() or similiar function listed in the manual (I don't think anyway).
No, because AFAIK all PHP arrays are associative; there is no distinction between arrays & hashes as in Perl.
I'm using mysql_fetch_array() and I want to foreach only the assoc. part of the array without using mysql_fetch_assoc(): foreach ($arr as $key=>$value) { echo "$key:$value"; }
If you really don't want to use mysql_fetch_assoc(), you can use the second parameter of mysql_fetch_array(): $arr = mysql_fetch_array($ResultHandle, MYSQL_FETCH_ASSOC); This functions exactly like mysql_fetch_assoc(), and is available in PHP >= 3.0.7. See http://www.php.net/manual/en/function.mysql-fetch-array.php for more info.
But of course it show both the indexed array and the assoc array contents. Is there an existing function to check this, or should I do one of these things inside the foreach loop: A) set $last=value and just check if $value = $lastval B) check if the $key is just a number or just a string C) $key += 1
Lastly, you _could_ just step through the array using each() in a loop, and use only every OTHER array entry...but this is really funky and you shouldn't do it this way anymore. -steve
?? Thanks, Henning Sittler www.inscriber.com
-- +------------------------------------------------------------------------+ | Steve Edberg sbedberg@ucdavis.edu | | University of California, Davis (530)754-9127 | | Programming/Database/SysAdmin http://pgfsun.ucdavis.edu/ | +------------------------------------------------------------------------+ | The end to politics as usual: | | The Monster Raving Loony Party (http://www.omrlp.com/) | +------------------------------------------------------------------------+

« previous php.general (#108312) next »