quelle Posted June 19, 2011 Share Posted June 19, 2011 Can anyone freaky help me im just too tired to figure out where im mistaking, pm me please ! Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/ Share on other sites More sharing options...
cssfreakie Posted June 19, 2011 Share Posted June 19, 2011 well some code would have been nice, but here is something that could be useful. just change the array values and you will see the effect. It probably could have been coded more efficient. $array = array( 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten' ); if(count($array)%2 === 0){ $var = (count($array)-1)/2; echo 'value is even middle numbers are '.$array[$var].'and '.$array[$var+1]; }else{ $var = count($array)/2; echo 'value is odd middle number is '.$array[$var]; } Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231928 Share on other sites More sharing options...
quelle Posted June 19, 2011 Author Share Posted June 19, 2011 Acutally we miss understood here, I meant middle by size like (67, 12, 105, 2) it would list middle numbers as 2 and 12 Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231932 Share on other sites More sharing options...
cssfreakie Posted June 19, 2011 Share Posted June 19, 2011 you mean even numbers?? Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231935 Share on other sites More sharing options...
quelle Posted June 19, 2011 Author Share Posted June 19, 2011 How do you mean even ? This will be an integer array Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231936 Share on other sites More sharing options...
Alex Posted June 19, 2011 Share Posted June 19, 2011 Then you can still use cssfreakie's solution, just use sort on the array beforehand. So something like: $arr = array(5, 92, 5, 42, 19); sort($arr); if(($s = sizeof($arr)) % 2 == 0) { echo "Middle values are: " . $arr[$s / 2] . " and " . $arr[$s / 2 - 1]; } else { echo "Middle value: " . $arr[floor($s / 2)]; } Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231937 Share on other sites More sharing options...
quelle Posted June 19, 2011 Author Share Posted June 19, 2011 Well yeah cool works thanks btw is it possible somehow to check middle value(s) in array if u got biggest and lowest value Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231938 Share on other sites More sharing options...
Alex Posted June 19, 2011 Share Posted June 19, 2011 Not sure what you're asking. Can you try to reformat your question? Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231939 Share on other sites More sharing options...
quelle Posted June 19, 2011 Author Share Posted June 19, 2011 //////////////////////////////////////////////////// function maximal($niz) { $mxm = $niz[0]; for ($i=0; $i < count($niz); $i++) { if($niz[$i]>$mxm) { $mxm = $niz[$i]; } } echo $mxm, "<br />"; } //////////////////////////////////////////////////// function minimal($niz) { $mnm = $niz[0]; for ($i=0; $i < count($niz); $i++) { if($niz[$i]>$mnm) { $mnm = $niz[$i]; } } echo $mnm, "<br />"; } //////////////////////////////////////////////////// Lets say i've got these 2 functions and im asking is it possible to check values in an array if they are different from these 2 and list them as middle values ? Btw this part here echo "Middle value: " . $arr[floor($s / 2)]; This will be used when $s is not dividable with 2 ? Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231945 Share on other sites More sharing options...
quelle Posted June 19, 2011 Author Share Posted June 19, 2011 Alex, under term of "middle values" i mean all values in an array whose are different from maximal and minimal value in that array Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231955 Share on other sites More sharing options...
Alex Posted June 19, 2011 Share Posted June 19, 2011 Depends on how you define "middle values". If you mean any values that aren't at the absolute ends, then yes you could do that (or in the case of only 3 or 4 elements). But if you're only looking for the middle 1 or 2 values inside an array of length greater than 4, then you'll need more information than just the maximum and minimum values of the array, without sorting of course. Btw this part here echo "Middle value: " . $arr[floor($s / 2)]; This will be used when $s is not dividable with 2 ? Yes, that's correct. For example, in the case of an array with length 7. $s / 2 will give us 3.5, and the floor function will round that value down, to 3. Giving us $arr[3], which is the middle value in that array (0, 1, 2, 3, 4, 5, 6). edit: Alex, under term of "middle values" i mean all values in an array whose are different from maximal and minimal value in that array Well, in that case it's not even necessary to know the min and max values of the array. You can simply sort the array, and create a new array ignoring the first and last elements. For example: $arr = array(5, 92, 5, 42, 19); sort($arr); print_r(array_slice($arr, 1, -1)); array_slice Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231956 Share on other sites More sharing options...
quelle Posted June 19, 2011 Author Share Posted June 19, 2011 Depends on how you define "middle values". If you mean any values that aren't at the absolute ends, then yes you could do that (or in the case of only 3 or 4 elements). But if you're only looking for the middle 1 or 2 values inside an array of length greater than 4, then you'll need more information than just the maximum and minimum values of the array, without sorting of course. Yeah thats exact what I mean, the second way was explained but im looking for the first way, doing it without array_splice. I was thinking about array_splice before this but it is a little bit difficult to understand even ur simple example, i would put (array_slice($arr, 0, -1); cuz i want to cut the first one and the last one ... Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231962 Share on other sites More sharing options...
cssfreakie Posted June 19, 2011 Share Posted June 19, 2011 well if you don't want to use array slice maybe try this: $myarray = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19); $min = 3; $max = 17; // so output should be all values between 3 and 17 //sort array sort($myarray); $newarray = array(); foreach($myarray as $value){ if($value<=$min ||$value>=$max){ //do nothing. }else{ $newarray[] = $value; // so here all values between min and max are assigned to a new array. } } var_dump($newarray); next time please define a bit more what you mean exactly. Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231964 Share on other sites More sharing options...
Alex Posted June 19, 2011 Share Posted June 19, 2011 If you check the manual for array_slice, you'll see what the parameters mean. In my example, I use 1 for the second parameter because it denotes which position to start taking values from. We want to skip the first, 0th element, so we start at 1. The third parameter is how far away from the end we want to stop. We want to stop collecting values 1 away from the end, so we choose -1. For example, you can try this code: $input = array("a", "b", "c", "d", "e"); print_r(array_slice($input, 1, -1)); // array('b', 'c', 'd') Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231965 Share on other sites More sharing options...
quelle Posted June 20, 2011 Author Share Posted June 20, 2011 If you check the manual for array_slice, you'll see what the parameters mean. In my example, I use 1 for the second parameter because it denotes which position to start taking values from. We want to skip the first, 0th element, so we start at 1. The third parameter is how far away from the end we want to stop. We want to stop collecting values 1 away from the end, so we choose -1. For example, you can try this code: $input = array("a", "b", "c", "d", "e"); print_r(array_slice($input, 1, -1)); // array('b', 'c', 'd') Aha thanks for a nice explanation, i have a better view on it from now on Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231970 Share on other sites More sharing options...
quelle Posted June 20, 2011 Author Share Posted June 20, 2011 well if you don't want to use array slice maybe try this: if($value<=$min ||$value>=$max) Thanks cssfreakie rly for ur help Btw this line of code could be used better as, right? while ($value => $min || $value <= $max) Quote Link to comment https://forums.phpfreaks.com/topic/239823-middle-value-in-array/#findComment-1231975 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.