+ 1
How to show middle number in array given the odd numbers like that (22,23,34,36,45), the output will be 34
i'm beginner in PHP please explain me the code
5 Answers
+ 3
Just an example
$numbers = array(22, 23, 34, 36, 45);
$length = count($numbers);
// count array element
if($length & 1) // is number of array element odd?
{
$middle = $numbers[$length / 2]; // get middle element
echo "Middle element is $middle";
}
else
{
echo 'No middle element found.';
}
0
Do you need help in PHP or in any of the tagged languages?
0
MD RASEL MAHMUD first find the size of your array and devide it by 2. After if the number has a decimal part . Example in your case here the size of the array is 5. So 5/2 =2.5. You need only the 2
So since you are using JS let's put x=2.5 and use Marh.trunc(x) and you will get 2.
And and use the 2 to print the element in the middle bkoz it will start counting from 0.
Note there is cases where there is no middle for example if the size of the array is divisible by 2 , l
0
thanks for correct answer, could you explain how to work single ampersand?
- 1
what do you mean by lagiages?