Guest Posted December 4, 2006 Share Posted December 4, 2006 I have a whole bunch of variables in an array about 30 and I need to find the lowest one but know which one it is not just the value so i can change it what would be the best way of doing this without a whole mess of if statments? I do not want to sort because then they wont be where I would need them. Quote Link to comment https://forums.phpfreaks.com/topic/29387-comparing/ Share on other sites More sharing options...
Zane Posted December 4, 2006 Share Posted December 4, 2006 can you rephrase that just a bitI almost understand it but not quite Quote Link to comment https://forums.phpfreaks.com/topic/29387-comparing/#findComment-134772 Share on other sites More sharing options...
Guest Posted December 4, 2006 Share Posted December 4, 2006 I have an array examplearray[0] = "100"array[1] = "300"array[2] = "24"array[3] = "40"I want to pick array[2] because it would be the lowesthow can i achieve this Quote Link to comment https://forums.phpfreaks.com/topic/29387-comparing/#findComment-134776 Share on other sites More sharing options...
obsidian Posted December 4, 2006 Share Posted December 4, 2006 Well, if you don't [b]have[/b] to keep the array in the same order, just run a sort() on it and echo the first value:[code]<?php$nums = array(100, 300, 24, 40);sort($nums);echo $nums[0];?>[/code]Otherwise, you'd need to do some sort of looping comparison like this:[code]$nums = array(100, 300, 24, 40);$small = $nums[0]; // default to the first valueforeach ($nums as $num) { $small = $num < $small ? $num : $small;}echo $small; // holds the smallest value in the array[/code]Hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/29387-comparing/#findComment-134783 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.