Jump to content

middle value in array


quelle

Recommended Posts

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];
}

Link to comment
Share on other sites

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)];
}

Link to comment
Share on other sites

////////////////////////////////////////////////////
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 ?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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')

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.