Re: Re: Array Sorting Headaches
| From: | Torsten Roehr | Date: | Mon, 19 Apr 2004 07:18:29 +0000 |
| Subject: | Re: Re: Array Sorting Headaches | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-183791@lists.php.net to get a copy of this message | ||
"Burhan Khalid" <phplist@meidomus.com> wrote in message
news:4083779C.6070300@meidomus.com...
> Torsten Roehr wrote:
>
> > "Burhan Khalid" <phplist@meidomus.com> wrote in message
> > news:40828989.8030802@meidomus.com...
> >
> >>Greetings everyone :
> >>
> >> Having a hard time with this one. I have a multi-dim array
> >>$foo[$x][$y]['key'], where $x and $y are numeric. Here is some sample
data
> >
> [ snipped ]
>
> >>I need to filter the results so that I get the latest expiry date for
> >>each product. The expires field actually contains a timestamp. So for
> >>the sample array above, the resultant array would have only keys 0 and
> >>2, filtering out all the rest. There are around 180+ main entries, with
> >>any number of sub entries, but each sub entry has the same four keys.
> >>
> >>Any ideas? Getting a rather large headache mulling over this.
> >
> >
> > Hi,
> >
> > as your structure is always the same you could just loop through all
> > elements and subelements and use unset() to remove the unwanted array
> > elements:
> >
> > foreach ($foo as $key => $value) {
> >
> > foreach ($value as $subkey => $subvalue) {
> >
> > // put your check logic here
> > if ($foo[$key][$subkey]['expires'] == '') {
> >
> > unset($foo[$key][$subkey]);
> > }
> > }
> > }
> >
> > What do you think?
>
> Well this is what I have right now, but the problem is the logic which
> checks to see for each domain which is the last expired date. I have
> three nested for loops right now, and not getting anywhere :(
Maybe it works if you put the expires value in a seperate array, sort it and
then you can get the key of the first one and use unset():
foreach ($foo as $key => $value) {
$tempArray = array();
foreach ($value as $subkey => $subvalue) {
// add expires value only to the temporary array for
sorting
$tempArray[$subkey] = $foo[$key][$subkey]['expires'];
}
// sort array by value descending
arsort($tempArray);
// get first (and therefore highest timestamp) key/value pair
$firstEntry = each($tempArray);
$notneededSubkey = $firstEntry['key'];
// now unset the array value with the not needed subkey
unset($foo[$key][$notneededSubkey]);
}
Have not tried it but may work. Hope you see my point.
Regards, Torsten Roehr