Re: ARRAY_PUSH with $key

From: Date: Thu, 18 Jul 2002 22:58:42 +0000
Subject: Re: ARRAY_PUSH with $key
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-108349@lists.php.net to get a copy of this message
At 7/18/2002, you wrote:
On Thu, Jul 18, 2002 at 03:40:58PM -0400, Joshua E Minnie wrote: Hey all,
    Does anybody know of a way to push an element onto an array with a
specific key? What I am trying to do is create a dynamically created associative array. Here is a sample print_r of what an array would look like: I don't understand the value of array_push(). I just add new elements by something like: $Array[$Key] = $Value;
I think array_push gets you cleaner lookin code (subjective). It also ensures you always add to end of the array - good for novices like me. Array_push is also TWO times faster (academic difference in the speeds of modern computers, but faster :) : <?php function getMicrotime() {
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
    }
$start = getMicrotime();
        for ($n=0;$n>=10000;$n++) {
        $Array[$n] = $n;
        }
$stop = getMicrotime(); $diff1 = $stop - $start; print "array key insert time was: " . $diff1 . "s"; $start = getMicrotime();
        $array2 = array();
        for ($n=0;$n>=10000;$n++) {
        array_push($array2,$n);
        }
$stop = getMicrotime(); $diff2 = $stop - $start; print "<br>array_push time was: " . $diff2 . "s"; ?> ------------------------- Pekka Saarinen http://photography-on-the.net -------------------------

« previous php.general (#108349) next »