Skip to content

Instantly share code, notes, and snippets.

@ichadhr
Created October 10, 2019 14:35
Show Gist options
  • Save ichadhr/651ff25a839ad4543cc6afd39878b92f to your computer and use it in GitHub Desktop.
Save ichadhr/651ff25a839ad4543cc6afd39878b92f to your computer and use it in GitHub Desktop.
```php
$array=[
['day'=>'11','movements'=>'1'],
['day'=>'11','movements'=>'1'],
['day'=>'11','movements'=>'1'],
['day'=>'12','movements'=>'1'],
['day'=>'12','movements'=>'1'],
['day'=>'12','movements'=>'1']
];
foreach($array as $row){ // iterate all rows
if(!isset($result[$row['day']])){ // if first occurrence of day...
$result[$row['day']]=$row; // save the full row with day as the temporary key
}else{ // if not the first occurrence of day...
$result[$row['day']]['movements']+=$row['movements']; // add movements value
}
}
var_export(array_values($result));
```
#### output:
```php
array (
0 =>
array (
'day' => '11',
'movements' => 3,
),
1 =>
array (
'day' => '12',
'movements' => 3,
),
)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment