Tuesday, August 4, 2009

PHP - Find min and max values in an array

/**
signature
array getMinMax( array )

returns an associative array with:
'min' -> smallest value in the given array
'max' -> greatest value in the given array

if the array is empty both fields are NULL
*/
function getMinMax($array)
{

reset($array);
if(
FALSE === key($array))
{
return array(
'min' => NULL, 'max' => NULL);
}

$min = $max = current($array);
$val = next($array);

while(
NULL !== key($array))
{
if(
$val > $max)
{
$max = $val;
}
elseif(
$val < $min)
{
$min = $val;
}
$val = next($array);
}
return array(
'min' => $min, 'max' => $max);
}


Usage:

$min_max_arr = getMinMax($myarray);

$min_val = $min_max_arr['min'];
$max_val = $min_max_arr['max'];

(found here -- http://www.codingforums.com/showthread.php?t=130494)

Example application -- If you had an array of dates, you could turn each entry into a timestamp, run the array of timestamps through this function, and get the minimum and maximum timestamps. Then you could simply convert the min and max timestamps back to a string (using something like strftime).

13 comments:

Anonymous said...

Nackte Amateure

Webcam Erotikchat

Cam Girls

unlock iphone 2g said...

I have an iPhone 3g with 3.1.2 and firmware 05.11.07. I have downloaded blackra1n to jailbreak and it worked great. But I am leaving to Hong Kong soon and I really hope it will work as an unlock too.
________________
[url=http://www.unlock-iphone.org]unlock iphone 2g[/url]

viagra online said...

this kind of topic is good for people to learn more about it, and that people should be every day less ignorant, and medicine for this is the reading of issues like this

Anonymous said...

new jersey state board of pharmacy newslwtter http://usadrugstoretoday.com/products/biaxin.htm physician dealing with compounding pharmacy [url=http://usadrugstoretoday.com/products/allopurinol.htm]allopurinol[/url]
pharmacy jobs nh http://usadrugstoretoday.com/products/betnovate.htm priceline pharmacy [url=http://usadrugstoretoday.com/products/tretinoin-cream-0-025-.htm]doctorate in pharmacy and health services[/url]

Anonymous said...

konrad zuse matrix movie http://moviestrawberry.com/films/film_riverworld/ the animal movie [url=http://moviestrawberry.com/films/film_stuart_little_3_call_of_the_wild/]stuart little 3 call of the wild[/url]
free downloadable xxx full lenth movie http://moviestrawberry.com/films/film_evil_under_the_sun/ best movie 1979 [url=http://moviestrawberry.com/films/film_her_cardboard_lover/]movie graham nc[/url]
transformers movie paramount stars http://moviestrawberry.com/films/film_diamond_dogs/ windows movie maker [url=http://moviestrawberry.com/films/film_honey_harvester/]honey harvester[/url]
black sex free movie sample http://moviestrawberry.com/films/film_the_last_confession_of_alexander_pearce/ illegal tender the movie [url=http://moviestrawberry.com/hqmoviesbygenres/download-genre_drama-movies/?page=1]black airplane movie[/url]
the simpsons movie dvd http://moviestrawberry.com/films/film_the_untouchables/ sandra bullock cyber movie [url=http://moviestrawberry.com/films/film_my_own_worst_enemy/]my own worst enemy[/url]
movie water fire earth reply http://moviestrawberry.com/films/film_dead_end/ legend of zelda movie [url=http://moviestrawberry.com/hqmoviesbyyear/year_1937_high-quality-movies/]the flying scotsman movie[/url]

Anonymous said...

agency online travel uk http://bank.bonox.eu/national-bank-of-monterial altitude travel [url=http://advance.bonox.eu/neosure-advance]neosure advance[/url]
top rated travel insurance http://bankruptcy.bonox.eu/new-bankruptcy-law-and-credit-card-debt europe motorhome travel [url=http://credit.bonox.eu/att-universal-credit-card-login]italy travel pictures[/url]
discount air travel to montreal http://business.bonox.eu/start-a-automatic-withdrawal-business travel food warmer [url=http://business.bonox.eu/business-practices]business practices[/url]
neuss germany travel information http://business.bonox.eu/measures-of-income-measurement-for-business travel trade linda mick azamara lines richard fain [url=http://finance.bonox.eu/my-way-finance]travel dog crates[/url]
travel jewerly tote http://bankruptcy.bonox.eu/hoe-will-bankruptcy-affect-my-chance-of-gettting-an-equity-loan national travel [url=http://credit.bonox.eu/credit-for-airline-mileage]credit for airline mileage[/url]
pet voyager travel crate http://finance.bonox.eu/barclay-finance-security amelia island travel info [url=http://visa.bonox.eu/tobago-visa-requirement]travel lodge wickliffe ohio[/url]

Anonymous said...

treasure coast pharmacy http://icepharmacy.in/bath-salt/homemade-craft-gift organic pharmacy [url=http://icepharmacy.in/cellulite/reducing-cellulite]reducing cellulite[/url]
jackson memorial hospital residency pharmacy miami http://icepharmacy.in/holistic/holistic-vets pharmacy technician home study program [url=http://icepharmacy.in/abdominal/fitness-exercise-care-abdominal-personal-and-equipment]math questions for pharmacy technicians[/url]
online usa pharmacy no prescription http://icepharmacy.in/exercise/exercise-example-and-pilates pharmacy technician schools kamloops [url=http://icepharmacy.in/acne/clera-light-acne-cure]clera light acne cure[/url]
list of pharmacy colleges http://icepharmacy.in/cellulite/remedies-for-cellulite pharmacy penegra weight loss silagra cumwithus c [url=http://icepharmacy.in/beauty/beauty-control-products]pre pharmacy programs[/url]
drugs online pharmacy http://icepharmacy.in/bodybuilding/bodybuilding-blender-recipes ainsworths homoeopathic pharmacy [url=http://icepharmacy.in/bodybuilding]bodybuilding[/url]
model data flow diagram of a pharmacy http://icepharmacy.in/homeopathy/polycythemia-and-homeopathy alltown pharmacy eatontown nj [url=http://icepharmacy.in/acupuncture/acupuncture-mass]online pharmacy flonase[/url]

Anonymous said...

westbury pharmacy three chopt http://health-life.kqv.in/angel-of-good-health sterling pharmacy stratford nj [url=http://blood-cells.kqv.in/complete-blood-count-weakness-dizziness-side-effects]complete blood count weakness dizziness side effects[/url]
how to pick a jury for a pharmacy trial http://diabetes-type.kqv.in/treatment-information-for-diabetes access pharmacy [url=http://health-care.kqv.in/veronica-michie-howard-county-health-department]internet pharmacy bulletin boiards[/url]
pharmacy resources http://heart-rate.kqv.in/the-function-of-the-heart harvey pharmacy perth [url=http://heart-medicine.kqv.in/heart-rate-chart-workout]heart rate chart workout[/url]
mexico nogales pharmacy http://canada-pharmacy.kqv.in/maple-leaf-pharmacy-seattle pharmacy laws schedule ii prescription [url=http://heart-rates.kqv.in/heart-rate-glycogen-fat]amana ia pharmacys[/url]
sample outline for pharmacy school essays http://heart-medicine.kqv.in/congestive-heart-failure-dogs orange belt pharmacy [url=http://health-centers.kqv.in/health-strange-death]health strange death[/url]
order tadalafil pharmacy http://canada-pharmacy.kqv.in/marrakech-argane-face-cream-pharmacy clinical provider pharmacy systems [url=http://women-health.kqv.in/springfield-health-department]longs auburn elm pharmacy[/url]

Anonymous said...

f4815a hp usb optical travel mouse manual guide style
microsoft reported earningsq207 mp3
pantie hose movie porn
romania travel tourism
alicia keys you and me together

Anonymous said...

ziggy time travel http://philodendron.dnst.info/philodendron-sizes-of philodendron sizes of [url=http://zantedeschia.dnst.info/zantedeschia-carmine-red]zantedeschia carmine red[/url]
http://pussy-willow.dnst.info/pussy-willow-bulk-lot pussy willow bulk lot rock band still water [url=http://tulipa.dnst.info/tulipa-greigii]tulipa greigii[/url]
[url=http://lisianthus.dnst.info/lisianthus-flowers]lisianthus flowers[/url] george clooney movie http://tuberose.dnst.info/jo-malone-tuberose jo malone tuberose
discount travel swinger resorts mexico http://sweet-william.dnst.info/sweet-william-family-restaurant sweet william family restaurant [url=http://wattle.dnst.info/red-wattle-swine]red wattle swine[/url]
http://wattle.dnst.info/spike-wattle spike wattle kissimmee state rock monument [url=http://myrtus.dnst.info/myrtus-reed-oregon]myrtus reed oregon[/url]

buy generic cialis online said...

Well worth to read this article, thanks for sharing this information. With this article you offered me got a chance to know about this, anyway i say Great Article! and waiting for you next article about this interesting subject.

phone cards said...

great source about code ) thanks

Anonymous said...

We have an occasional patient with OHSS in the hospital and once in a while we have an ectopic pregnancy that requires laparoscopy but most of the time there are not a lot of medical surprises. Canine Cataracts