Reputation: 2291
I am trying to count the duplicate keys from array $new_result_months
, remove them and update the value of $new_result_users
with the matching key. if I would do an array_combine
every key should match the value. I really tried but did not managed to think of a version.
$new_result_months = array(
'Dec 2013',
'Jan 2014',
'Feb 2014',
'Mar 2014',
'Apr 2014',
'May 2014',
'Jun 2014',
'Jul 2014',
'Aug 2014',
'Sep 2014',
'Oct 2014',
'Oct 2014', // remove one duplicate key and keep unique one
'Nov 2014',
'Nov 2014', // remove one duplicate key and keep unique one
'Nov 2014', // remove one duplicate key and keep unique one
'Dec 2014'
);
$new_result_users = array(
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
1,
1,
0
);
EXPECTED OUTPUT
$new_result_months = array(
'Dec 2013',
'Jan 2014',
'Feb 2014',
'Mar 2014',
'Apr 2014',
'May 2014',
'Jun 2014',
'Jul 2014',
'Aug 2014',
'Sep 2014',
'Oct 2014',
'Nov 2014',
'Dec 2014'
);
$new_result_users = array(
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1, // needs to match with month oct 2014 and update it to 1
2, // needs to match with month nov 2014 and update it to 2
0
);
Upvotes: 0
Views: 26
Reputation: 1661
First you count the duplicated values with array_count_values
:
$aux = array_count_values($new_result_months);
The new created array $aux
will have this values:
['Dec 2013'] => 1
['Jan 2014'] => 1
['Feb 2014'] => 1
['Mar 2014'] => 1
['Apr 2014'] => 1
['May 2014'] => 1
['Jun 2014'] => 1
['Jul 2014'] => 1
['Aug 2014'] => 1
['Sep 2014'] => 1
['Oct 2014'] => 2
['Nov 2014'] => 3
['Dec 2014'] => 1
Then, with a foreach loop, you can create the new arrays that you want:
foreach($aux as $key => $value) {
$new_result_months[] = $key;
$new_result_users[] = $value;
}
Upvotes: 1