Created
October 10, 2019 14:35
-
-
Save ichadhr/651ff25a839ad4543cc6afd39878b92f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```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