How to show middle number in array given the odd numbers like that (22,23,34,36,45), the output will be 34 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

14th Nov 2020, 9:27 PM
MD RASEL MAHMUD
MD RASEL MAHMUD - avatar
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.'; }
14th Nov 2020, 11:47 PM
Ipang
0
Do you need help in PHP or in any of the tagged languages?
14th Nov 2020, 9:33 PM
Davide
Davide - avatar
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
14th Nov 2020, 9:40 PM
Khaled ^^ خالد القريشي‎
Khaled ^^ خالد القريشي‎ - avatar
0
thanks for correct answer, could you explain how to work single ampersand?
15th Nov 2020, 9:25 AM
MD RASEL MAHMUD
MD RASEL MAHMUD - avatar
- 1
what do you mean by lagiages?
14th Nov 2020, 9:38 PM
MD RASEL MAHMUD
MD RASEL MAHMUD - avatar