Solved

Comparing values from a while loop....

Posted on 2011-09-02
9
199 Views
Last Modified: 2012-06-21
So i have a while loop which does math on several records.

After i have completed math on each record, how can i store each value to sort highest to lowest after the while loop has completed?
0
Comment
Question by:blink10
  • 3
  • 3
  • 3
9 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36476184
Put the values into an array, and you can sort them later.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36476186
Check this link for the ways that PHP can sort arrays of information:
http://us3.php.net/manual/en/array.sorting.php
0
 
LVL 10

Expert Comment

by:Ultrus
ID: 36476234
Try something like this:

<?php
$magicNumber = 1;
$someNums = array(22,45,24,67);
$combinedArray = array();
$resultArray = array();

foreach($someNums as $someNum) {
  $result = $someNum + $magicNumber;
  $resultArray[] = $result;
  $combinedArray[] = array('result' => $result, 'origNum' => $someNum);
}

array_multisort($resultArray, SORT_DESC, $combinedArray);

print 'Drum roll please... ' . $combinedArray[0]['result'] . ' with an original number of ' . $combinedArray[0]['origNum'];

print '<pre>';
print_r($combinedArray);
print '</pre>';
?>

Open in new window


Let me know if that helps inspire something. :)

Best regards,

Chris
0
 

Author Comment

by:blink10
ID: 36476242
Chris thanks for the solution, but I think I am very close to what i need with my code i wrote since i posted 20 minute ago.


One last problem i am having, i am creating an associate array, sorting it fine, but how can i make it so that i can just choose the lowest value without knowing the corresponding key.

In other words, i just need to lowest variable to do more math with, how can i just select it?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:Ultrus
ID: 36476253
Since you sorted it Descending (highest value first), try this:

<?php
print $myValues[count($myValues) - 1];
?>

Open in new window


That gives the last index.
0
 

Author Comment

by:blink10
ID: 36476274
No luck....

I used:
            foreach ($allcoupons as $key => $val) {
                 echo "$key = $val\n";
                  }

To determine my array after sorting, the above produced:
Round1 = 23.2 Round2 = 28.2744 Round3 = 38.88

Then I wrote:

            echo "<b>Best Price: </b>";
            
            print $allcoupons[count($allcoupons) - 1];

And got nothing, thoughts?
0
 
LVL 10

Accepted Solution

by:
Ultrus earned 250 total points
ID: 36476286
Ahhh. Your keys are not numeric. That's why.

Try

<?php
print end($allcoupons);
?>

Open in new window


(never tried it before)
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 36476314
Please read this over carefully.  If you do not know 100% what any of the functions accomplish, look them up on PHP.net and read the descriptions, inputs, outputs (return values) and user-contributed notes.  Then post back with any questions.
http://www.laprbass.com/RAY_temp_blink10.php

You might also want to get this book and give yourself a little time to work through it.  Very readable with great examples.
http://www.sitepoint.com/books/phpmysql4/

Best of luck with your project, ~Ray
<?php // RAY_temp_blink10.php
error_reporting(E_ALL);
echo "<pre>";

// FROM THE POST AT EE
// Round1 = 23.2 Round2 = 28.2744 Round3 = 38.88

// CREATE SOME SIMULATED TEST DATA
$arr = array
( 'Round1' => 23.2
, 'Round2' => 28.2744
, 'Round3' => 38.88
)
;

// SHOW THE SIMULATED TEST DATA
var_dump($arr);

// ORDER IT IN A RANDOM MANNER
shuffle($arr);

// SHOW THE SHUFFLED DATA
var_dump($arr);

// SORT BY VALUES DESCENDING
arsort($arr);
reset($arr);

// SHOW THE LARGEST VALUE IN THE ARRAY
var_dump(current($arr));

Open in new window

0
 

Author Closing Comment

by:blink10
ID: 36477049
thanks for the help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Paypal 502 Error 3 74
test if query has no results 2 19
how to handle $x_amount to payment gateway in php - stumped. 14 14
array_values - reorder after unset? 5 12
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now