Welcome Guest, Not a member yet? Register   Sign In
Remove duplicates from Multi-dimensional array
#2

[eluser]jedd[/eluser]
[quote author="BiSHGoD" date="1249960070"]Hello.
I've been using CI for a few weeks now and I'm really liking it. I wonder if there is any function/library to clean up a multidimensional array based on a specific key.

On PHP.net I see a lot of fixes for this problem but none that I think apply to me.[/quote]

Hi BiSHGoD and welcome to the CI Forums.

I think I can safely say that CI won't make this any easier - it's a pretty straightforward data cleansing problem you have - you either fix it in the right place (on the way into your database) or you fix it using MySQL and/or PHP.

My gut feel is that you write or borrow an array sort function that'll sort by the thing you want to consider unique .. and then scan through it looking to see if the next is a copy of the current, and if so, don't copy it to your new array. Messy .. I think. There may be more optimum solutions, of course.

Quote:
Code:
Array
(
    [0] => Array
        (
            [lid] => 4
            [timestamp] => 2009-08-10 14:46:34
            [number] => CI00101
            [location] => 358 A
        )

    [1] => Array
        (
            [lid] => 5
            [timestamp] => 2009-08-10 14:46:38
            [number] => CI00101
            [location] => 358 A
        )

    [2] => Array
        (
            [lid] => 9
            [timestamp] => 2009-08-10 15:54:30
            [number] => CI00101
            [location] => 358 B
        )
)

I want to remove dupliates based on the "location" key. So [2] should be removed, since it has the same value, 358 B, as [0].

When one's providing example data .. it's always important to make sure it says what you think it says. Wink


Quote:This data is from a mysql database..so if we can do this in the select statement that would work as well (right now it's 'select * from log where number = CI00101')

You could possibly try to work some magic with DISTINCT, or LIMIT1 .. but this is ugly, and would produce unpredictable results - depending on what fields you've got indexed, or how the table is sorted.

My gut feel is, if you can't get clean data into your database, you're looking at solving this problem at a PHP level on the way out. Any update to your DB suggests a batch process - but again, this also implies some sanity to your data from that point on (or you keep on cleaning it after the fact).


Messages In This Thread
Remove duplicates from Multi-dimensional array - by El Forum - 08-10-2009, 04:07 PM
Remove duplicates from Multi-dimensional array - by El Forum - 08-10-2009, 04:23 PM
Remove duplicates from Multi-dimensional array - by El Forum - 08-10-2009, 04:32 PM
Remove duplicates from Multi-dimensional array - by El Forum - 08-11-2009, 08:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB