Jump to content

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!

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.