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

[eluser]BiSHGoD[/eluser]
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.

I have a multidimensional array with a few keys that might be duplicate, which can be. I have one single key that I want to be unique though.

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].

You'll notice that "number" is the same on all of them, that shouldn't check for duplicates based on that key.

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')

Thanks!
#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).
#3

[eluser]MrCrooked[/eluser]
You should always try to prevent such things when you insert them in the database, however it's sometimes not possible. When it's not possible you should try to help yourself by getting the data from your database the "smart" way. Use SQL wisely when selecting data from the database. You could use GROUP BY/DISTINCT. Your final resort is PHP.
#4

[eluser]BiSHGoD[/eluser]
Thanks guys!

You are right, going into my database is the best way. I'm actually getting this data from a large log file of all my transactions, so duplicates are valid. For this view I just want to show unique locations, other places I'll show non-unique ones.

In the end this is what worked:
Code:
$this->db->select('number,location,lid,timestamp);
        $this->db->group_by('location');
        $this->db->where('number',CI00101);
        $query = $this->db->get('log');

Thanks for the GROUP BY/DISTINCT suggestion, haven't used that with mysql before.




Theme © iAndrew 2016 - Forum software by © MyBB