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
Share on other sites

SELECT DISTINCT name_field, field2, field3 ORDER BY name_field

 

Hi Pikachu2000 and thanks for your answer but I'm not having an SQL problem here, this is a php problem! This is a php array, and not a table in a database!

Link to comment
Share on other sites

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
        )

)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.