Jump to content

2d array duplicate removal procedure


IER506

Recommended Posts

Good evening co-coders. I'm facing some problems while trying to remove duplicates from one array. Here is how my array looks

like:

 

Array (
[0] => Array ( [name] => george [days] => 20 ) 
[1] => Array ( [name] => george [days] => 10 )
[2] => Array ( [name] => nickie [days] => 20 )
) 

 

... and after the conversion I want my array to look like this:

 

Array (
[0] => Array ( [name] => george [days] => 20 ) 
[1] => Array ( [name] => nickie [days] => 20 )
) 

 

As you've noticed I just want to keep the distinct values of the name but with the larger value of the days! Any ideas?

 

PS:My table is sorted if that helps (NAME ASC, DAYS ASC).

 

Thanks in advance for your help!

 

 

Link to comment
https://forums.phpfreaks.com/topic/207958-2d-array-duplicate-removal-procedure/
Share on other sites

Oh, I just assumed the data was coming from a DB query based on the "NAME ASC, DAYS ASC" comment you had in the OP . . . However, have a look at array_unique(). It may work for what you're doing.

From the sounds of your problem it definitely seems like it can and should be solved through MySQL.

 

You can try:

SELECT name, MAX(days) as max_days FROM table GROUP BY name

 

In the case that I'm misunderstanding and this really can't be achieved through MySQL here's a PHP solution:

 

$arr = array(
array(
	'name' => 'george',
	'days' => 20
), 
array(
	'name' => 'george',
	'days' => 10
),
array(
	'name' => 'nickie',
	'days' => 20 
)
);

function getNameIndex($arr, $name) {
foreach($arr as $key => $val) {
	if($val['name'] == $name) {
		return $key;
	}
}
return false;
}

$new = array();
foreach($arr as $key => $val) {
if(($index = getNameIndex($new, $val['name'])) !== false) {
	if($new[$index]['days'] < $val['days']) {
		$new[$index]['days'] = $val['days'];
	}
} else {
	$new[] = $val;
}
}

echo "<pre>" . print_r($new, true) . "</pre>";

 

Output:

Array
(
    [0] => Array
        (
            [name] => george
            [days] => 20
        )

    [1] => Array
        (
            [name] => nickie
            [days] => 20
        )

)

I agree to do it in the query, but if your array is already sorted, then here's one way:

 

foreach(array_reverse($array) as $row) {
$new[$row['name']] = $row;
}
$array = array_reverse(array_values($new));

You forgot to take into account that he wants the largest day value to be used if multiple exist.

You forgot to take into account that he wants the largest day value to be used if multiple exist.

Yes, they said:

PS:My table is sorted if that helps (NAME ASC, DAYS ASC).

 

And I said:

if your array is already sorted

I agree to do it in the query, but if your array is already sorted, then here's one way:

 

foreach(array_reverse($array) as $row) {
$new[$row['name']] = $row;
}
$array = array_reverse(array_values($new));

 

That was exactly what i was trying to do! Thank you all for your help and support guys! When my project is completed I'll feedback with the results!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.