[PHP] ARRAY_PUSH with $key
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: Array ( [default] = css/default.css [forms] = css/forms.css ) I have tried array_push, but can't seem to get the results I am looking for. i.e.: ? array_push($samp, $key=$value); // this throws an error array_push($samp, array($key=$value)); // doesn't give the results i'm looking for /* output from second array_push Array ( [0] = Array ( [default] = css/default.css ) [1] = Array ( [forms] = css/forms.css ) )*/ ? If anyone can point me in the right direction, I would appreciate any help you can give me. -- Joshua E Minnie/Webmaster [EMAIL PROTECTED] Phone: 616.276.9690 Fax: 616.342.8750 Nextel: 616.862.2847 Don't work for recognition, but always do work worthy of recognition. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] ARRAY_PUSH with $key
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; --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] ARRAY_PUSH with $key
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=1;$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=1;$n++) { array_push($array2,$n); } $stop = getMicrotime(); $diff2 = $stop - $start; print brarray_push time was: . $diff2 . s; ? - Pekka Saarinen http://photography-on-the.net - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] ARRAY_PUSH with $key
On Fri, Jul 19, 2002 at 01:58:42AM +0300, Pekka Saarinen wrote: 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 :) : AH! But your test is flawed. Reorder things so the push is first and that'll be slower. In addition, push() is more like setting $Array[] rather than $Array[$n]. So, here's my test, including some arithmetic calculation changes: pre ?php function getMicrotime() { list($usec, $sec) = explode(' ', microtime() ); return bcadd($usec, $sec, 6); } # ROUND 1 $Array = array(); $start = getMicrotime(); for ($n=0;$n=1;$n++) { $Array[$n] = $n; } $stop = getMicrotime(); echo 'key num: ' . (bcsub($stop, $start, 6)) . \n; $Array = array(); $start = getMicrotime(); for ($n=0; $n=1; $n++) { array_push($Array, $n); } $stop = getMicrotime(); echo 'push: ' . (bcsub($stop, $start, 6)) . \n; $Array = array(); $start = getMicrotime(); for ($n=0; $n=1; $n++) { $Array[] = $n; } $stop = getMicrotime(); echo 'key blank: ' . (bcsub($stop, $start, 6)) . \n; # ROUND 2 $Array = array(); $start = getMicrotime(); for ($n=0;$n=1;$n++) { $Array[$n] = $n; } $stop = getMicrotime(); echo 'key num: ' . (bcsub($stop, $start, 6)) . \n; $Array = array(); $start = getMicrotime(); for ($n=0; $n=1; $n++) { array_push($Array, $n); } $stop = getMicrotime(); echo 'push: ' . (bcsub($stop, $start, 6)) . \n; $Array = array(); $start = getMicrotime(); for ($n=0; $n=1; $n++) { $Array[] = $n; } $stop = getMicrotime(); echo 'key blank: ' . (bcsub($stop, $start, 6)) . \n; ? /pre Results... key num: 0.000301 push: 0.000145 key blank: 0.000135 key num: 0.000136 push: 0.000135 key blank: 0.000135 The times compared to each other varied each time I reran the test. --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php