Re: array_push but with key, value pairs

From: Date: Tue, 17 Apr 2001 10:20:57 +0000
Subject: Re: array_push but with key, value pairs
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-48932@lists.php.net to get a copy of this message
Joseph. "Joseph Blythe" <joe.blythe@binarylogic.com.au> wrote: ... > Ok fine, I now have an associative array with a numeric index, but > hangon I wanted to push the key => value into the array. How would I do > this, I have tried a couple of things but am having some really crazy > results ;-) 'array_push' is only useful for numerically-indexed arrays. It's basically a shortcut for getting the length of the array and putting a value in the last available spot in an array. It treats the array like a stack. In an associative array (or hash), the array is indexed by strings, so the order you input them in is irrelevant (for the most part). I think in PHP you can actually retrieve the values in the same order as you put them in, but this does not have to be the case. Hash keys should be arbitrarily ordered. > $input = array(); > while ( list($key, $val) = each($HTTP_POST_VARS) ) { > if ( $key != "Submit" ) { > array_push($input, $val); > } > } Instead of 'array_push', do this: $input[$key] = $val; Dean http://hall.apt7.com

« previous php.general (#48932) next »