|
Posted by mantrid on 12/31/07 21:10
"Ivαn Sαnchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
wrote in message news:flb9cc$s85$1@hercules.cohp1...
> mantrid wrote:
>
> > it is missing out any records with duplicate selldates. How do I tell it
> > to include duplicates?
>
> You cannot, it's how ordered maps work.
>
> You can, however, use a custom ordering function (see usort() ) and forget
> creating a second array altogether.
>
> --
> ----------------------------------
> Ivαn Sαnchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
>
> MSN:i_eat_s_p_a_m_for_breakfast@hotmail.com
> Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net
Thanks again Ivan
Looked at usort and the example given. I applied it to my situation and came
up with
*******************
while($row = mysql_fetch_array($transactions)) {
extract($row);
$data[ $row['heldlength'] ] = $row;
}
ksort($data);
$accum = 0;
$heldorder = 0;
foreach($data as $key=>$item) {
if ($item['profnloss'] > 0){
$data[$key]['accum'] = $accum += $item['profnloss'];
$data[$key]['heldorder'] = $heldorder += 1;
}else{
$data[$key]['accum'] = $accum;
$data[$key]['heldorder'] = $heldorder += 1;
}
}
function usort_cmp($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
usort($data, "usort_cmp");
******************************
Too my suprise, it works perfectly. Ever heard the one where you give a
group of monkeys pens and paper and given enough time they will write the
complete works of shakespere. I think something similar has happened here. I
ve solved it but havent a clue how?
I am particularly puzzled by what the function
function usort_cmp($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
is doing exactly. Particularly where argument $b comes from, and how it and
the returned values -1,0 and 1 are affecting the ordering of the array.
If anyone wants to explain this to me in simple english I'd be grateful. If
not, no worries Im just glad it works. Been on this for 4 days now.
Ian
Navigation:
[Reply to this message]
|