So I've created an array based on posted information (a big list of numbers separated by lines which has then been exploded)
So this array may contain numbers from 0-100

or whatever

So I have one array of numbers

Now for each number I want to find out many numbers are BELOW it in the entire array

So what I do is use foreach twice.

``````<?php
foreach(\$myarray as \$n => \$v) {
\$total="";
foreach(\$myarray as \$name => \$value) {
if(\$value<\$v) {
\$total=\$total+1;
}
}
echo \$total;
}
?>``````

Right.
I know what the problem is but I don't know how to fix it!

When I use \$value<\$v, it gives the wrong total.

What it does, say \$value = 20, and \$v = 4, it thinks 20 is smaller than 4 - it's not a clever number. Say \$v was 1, it would think it was smaller. It doesn't take the whole number into consideration.

It only doesn't work when \$v is given as a variable, and not a static number.

if I did this...

``````<?php
foreach(\$myarray as \$name => \$value) {
if(\$value<4) {
\$total=\$total+1;
}
}
echo \$total;
?>``````

It would work.

I'm not sure how well I've explained this but, yeah. Any ideas? Cheers!

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by nav33n

Fixed it

I had to user number_format() on the numbers :)

!!!

not clear... but intresting to reply..

``````<?php
\$arr = array(11,25,3,47,775,96,70,82,29,10);
\$outputArr  = Array();
foreach(\$arr as \$key => \$value) {
\$count++;
\$outputArr[\$value] = count(\$arr)-\$count;
}
echo "<pre>";
print_r(\$outputArr);
?>``````

.. try this printed o/p... if its not satisfying u.. pls explain d probs... with example

@vinothkumarc, The OP fixed the problem himself using the function number_format. :)