IER506 Posted July 16, 2010 Share Posted July 16, 2010 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! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 16, 2010 Share Posted July 16, 2010 SELECT DISTINCT name_field, field2, field3 ORDER BY name_field Quote Link to comment Share on other sites More sharing options...
IER506 Posted July 17, 2010 Author Share Posted July 17, 2010 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! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 17, 2010 Share Posted July 17, 2010 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. Quote Link to comment Share on other sites More sharing options...
IER506 Posted July 17, 2010 Author Share Posted July 17, 2010 Thanks again Pikachu , but unfortunately array_unique is not doing the job for 2d tables. I've tried to create a function based on it, but with no results. Can anyone suggest and idea? Quote Link to comment Share on other sites More sharing options...
Alex Posted July 17, 2010 Share Posted July 17, 2010 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 ) ) Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 17, 2010 Share Posted July 17, 2010 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)); Quote Link to comment Share on other sites More sharing options...
Alex Posted July 17, 2010 Share Posted July 17, 2010 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. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 17, 2010 Share Posted July 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
Alex Posted July 17, 2010 Share Posted July 17, 2010 Ah, if the data is ordered that way then yeah, that's a better solution. Quote Link to comment Share on other sites More sharing options...
IER506 Posted July 18, 2010 Author Share Posted July 18, 2010 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.