Created
April 2, 2015 17:00
-
-
Save anonymous/8749b58b07e7e489cd6a 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 | |
$arr = array( | |
array("id"=>1,'num'=>5,'period'=>3,'price'=>5), | |
array("id"=>2,'num'=>10,'period'=>3,'price'=>5), | |
array("id"=>3,'num'=>15,'period'=>9,'price'=>20) | |
); | |
$tmpData = array(); | |
foreach($arr as $item) { | |
if(isset($tmpData[$item['period']])) { | |
if(isset($tmpData[$item['period']][$item['price']])) { | |
$tmpData[$item['period']][$item['price']] += $item['num']; | |
} else { | |
$tmpData[$item['period']][$item['price']] = $item['num']; | |
} | |
} else { | |
$tmpData[$item['period']] = array($item['price'] => $item['num']); | |
} | |
} | |
$outputData = array(); | |
foreach($tmpData as $period => $itemArr) { | |
foreach($itemArr as $price => $num) { | |
$data = array(); | |
$data['num'] = $num; | |
$data['period'] = $period; | |
$data['price'] = $price; | |
$outputData[] = $data; | |
} | |
} | |
print_r($outputData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment