| | |
array numbers
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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...
It would work.
I'm not sure how well I've explained this but, yeah. Any ideas? Cheers!
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 Syntax (Toggle Plain Text)
<?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 Syntax (Toggle Plain Text)
<?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!
Last edited by tominic; Mar 15th, 2009 at 6:18 pm.
•
•
Join Date: Apr 2008
Posts: 28
Reputation:
Solved Threads: 3
not clear... but intresting to reply..
.. try this printed o/p... if its not satisfying u.. pls explain d probs... with example
PHP Syntax (Toggle Plain Text)
<?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); ?>
![]() |
Other Threads in the PHP Forum
- Previous Thread: question regarding adding a live search to a form
- Next Thread: php4 to php5 problem, I think!
Views: 365 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array autosuggest beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email error explodefunction file files folder form forms function functions google hack href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search searchbox select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web website xml youtube







