Re: [PHP] Re: Array Sorting Headaches
Torsten Roehr wrote: Burhan Khalid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 :( -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Array Sorting Headaches
Burhan Khalid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Torsten Roehr wrote: Burhan Khalid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Array Sorting Headaches
Torsten Roehr wrote: Burhan Khalid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Torsten Roehr wrote: Burhan Khalid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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. Well, it does some sorting, but not quite what I'm after :( I've managed to get the list so that all the (sub) entries are sorted in the correct order. Now its just a matter of finding the highest expire date for /each/ domain, and delete the other entries for that domain. So, in the end, all that's left is one entry per domain, with its latest expire date. Hopefully this makes it a bit clearer. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Array Sorting Headaches
Well, it does some sorting, but not quite what I'm after :( I've managed to get the list so that all the (sub) entries are sorted in the correct order. Now its just a matter of finding the highest expire date for /each/ domain, and delete the other entries for that domain. So, in the end, all that's left is one entry per domain, with its latest expire date. Hopefully this makes it a bit clearer. OK, so it's just the other way round - I see. Instead of deleting the entry with the highest expiry date we delete all the others: 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); /* new stuff starts here */ // remove the entry with the latest expiry (the first array element) array_push($tempArray); // now unset all remaining/not needed entries foreach ($tempArray as $tempKey = $tempValue) { unset($foo[$key][$tempKey]); } } By the way, isn't there a way to filter this stuff BEFORE or WHILE the whole array is created? Regards, Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Array Sorting Headaches
Torsten Roehr wrote: Well, it does some sorting, but not quite what I'm after :( I've managed to get the list so that all the (sub) entries are sorted in the correct order. Now its just a matter of finding the highest expire date for /each/ domain, and delete the other entries for that domain. So, in the end, all that's left is one entry per domain, with its latest expire date. Hopefully this makes it a bit clearer. OK, so it's just the other way round - I see. Instead of deleting the entry with the highest expiry date we delete all the others: 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); /* new stuff starts here */ // remove the entry with the latest expiry (the first array element) array_push($tempArray); // now unset all remaining/not needed entries foreach ($tempArray as $tempKey = $tempValue) { unset($foo[$key][$tempKey]); } } By the way, isn't there a way to filter this stuff BEFORE or WHILE the whole array is created? Thanks for the help. I'll give that snippet a try and see what comes out. The array is a result of a rather nasty SQL statement running against a database (which I didn't design). Unfortunately, the way the database is designed, its very difficult to extract the domain information. I am in the process of rewriting the entire thing as a Java application, hopefully that will make things a bit clearer, since I'll also be refactoring the database schema. Many thanks, Burhan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Array Sorting Headaches
OK, so it's just the other way round - I see. Instead of deleting the entry with the highest expiry date we delete all the others: 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); /* new stuff starts here */ // remove the entry with the latest expiry (the first array element) array_push($tempArray); // now unset all remaining/not needed entries foreach ($tempArray as $tempKey = $tempValue) { unset($foo[$key][$tempKey]); } } By the way, isn't there a way to filter this stuff BEFORE or WHILE the whole array is created? Thanks for the help. I'll give that snippet a try and see what comes out. The array is a result of a rather nasty SQL statement running against a database (which I didn't design). Unfortunately, the way the database is designed, its very difficult to extract the domain information. I am in the process of rewriting the entire thing as a Java application, hopefully that will make things a bit clearer, since I'll also be refactoring the database schema. Please let me know if it works. I'm sure there's a more elegant way to do it. Many thanks, Burhan You're welcome ;) Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Array Sorting Headaches
Burhan Khalid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 : Array ( [$x] = Array ( [0] = Array ( [invoiceid] = 11842 [product] = myproduct [domain] = foo.net [expires] = February 28, 2004 ) [1] = Array ( [invoiceid] = 10295 [product] = myotherproduct [domain] = foo.net [expires] = December 9, 2003 ) [2] = Array ( [invoiceid] = 10202 [product] = product1 [domain] = foo.bar [expires] = January 19, 2003 ) [3] = Array ( [invoiceid] = 10005 [product] = product2 [domain] = foo.bar [expires] = December 11, 2002 ) ) ) 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? Regards, Torsten Roehr Thanks again, Burhan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Array Sorting
I found this Great PHP Starter site check it out http://www.htmlite.com/PHPintro.php Ashley M. Kirchner [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I have an array that looks like this: $i = 0; $item[$i] = array( 'link' = 'http://...', 'image' = '/images/image.jpg', 'title' = 'some title', 'price' = '$14.00', 'cat'= 'Frames', 'author' = 'Pinochio', 'artist' = '', 'asin' = '010101', 'manufacturer' = 'Post' ); $i++; $item[$i] = array( 'link' = 'http://...', 'image' = '/images/something.jpg', 'title' = 'this is fun', 'price' = '$2.99', 'cat'= 'Card', 'author' = 'Mickey', 'artist' = '', 'asin' = '1116221', 'manufacturer' = 'Kraft' ); $i++; etc., etc. I would like to sort $items based on the manufacturer of each array within. So that, in the above example, the second one would come before the first. Is there a way to do that? -- H| I haven't lost my mind; it's backed up on tape somewhere. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / WebSmith . 800.441.3873 x130 Photo Craft Laboratories, Inc.. 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Array sorting
never done it, but check out: http://www.php.net/manual/en/function.array-multisort.php On Sat, 8 Feb 2003, Zydox wrote: My question is if anyone have any idé on how to sort this array so that the names and ages are sorted after Close Friend, Friend, Wife and last Family... $A[] = array (Nils, 23, Friend); $A[] = array (Emma, 21, Friend); $A[] = array (Cassie, 21, Wife); $A[] = array (Zydox, 23, Friend); $A[] = array (Patrick, 24, Close Friend); $A[] = array (Kalle, 40, Family); $A[] = array (John, 10, Close Friend); $A[] = array (Anna, 12, Friend); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php