array numbers

Thread Solved

Join Date: Mar 2009
Posts: 2
Reputation: tominic is an unknown quantity at this point 
Solved Threads: 0
tominic tominic is offline Offline
Newbie Poster

array numbers

 
0
  #1
Mar 15th, 2009
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.
  1. <?php
  2. foreach($myarray as $n => $v) {
  3. $total="";
  4. foreach($myarray as $name => $value) {
  5. if($value<$v) {
  6. $total=$total+1;
  7. }
  8. }
  9. echo $total;
  10. }
  11. ?>

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...
  1. <?php
  2. foreach($myarray as $name => $value) {
  3. if($value<4) {
  4. $total=$total+1;
  5. }
  6. }
  7. echo $total;
  8. ?>

It would work.

I'm not sure how well I've explained this but, yeah. Any ideas? Cheers!
Last edited by tominic; Mar 15th, 2009 at 6:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: tominic is an unknown quantity at this point 
Solved Threads: 0
tominic tominic is offline Offline
Newbie Poster

Re: array numbers

 
0
  #2
Mar 15th, 2009
Fixed it

I had to user number_format() on the numbers

!!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 28
Reputation: vinothkumarc is an unknown quantity at this point 
Solved Threads: 3
vinothkumarc vinothkumarc is offline Offline
Light Poster

Re: array numbers

 
0
  #3
Mar 16th, 2009
not clear... but intresting to reply..
  1. <?php
  2. $arr = array(11,25,3,47,775,96,70,82,29,10);
  3. $outputArr = Array();
  4. foreach($arr as $key => $value) {
  5. $count++;
  6. $outputArr[$value] = count($arr)-$count;
  7. }
  8. echo "<pre>";
  9. print_r($outputArr);
  10. ?>
.. try this printed o/p... if its not satisfying u.. pls explain d probs... with example
Thanks & Regards,
VinothKumar C
post2vinoth@gmail.com
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: array numbers

 
0
  #4
Mar 16th, 2009
@vinothkumarc, The OP fixed the problem himself using the function number_format.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 365 | Replies: 3
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC