【发布时间】:2016-02-18 12:47:00
【问题描述】:
我真的很难过滤我的数据。我知道如何访问数组的某些部分,但在如何过滤方面很吃力。我知道如何在 SQL SUM & count 中做到这一点),但在涉及 PHP 数组时不知道,请帮忙。
我需要过滤存储在变量 $res 中的一些数据。
我需要查看每个 'REF' 出现了多少次以及每个 ref 的总 'LCL_D_CBM' 所有这些,其中“TERR”等于“TERR1”。
foreach ($res as $res1){
if ($res1["TERR"] == "TERR1"){
//the individual ref
//$res1['REF'];
// the coutn of how many times each ref appears
//echo count($res1['REF']);
//sum
//echo array_sum($res1['LCL_D_CBM']);
}
}
需要结果像 -
CEA
3
2561
CEF
456
135
var_dump($res); gives me below
array(350) {
[0]=> array(7) { [0]=> string(3) "CEA" ["REF"]=> string(3) "CEA" [1]=> string(5) "1.080" ["LCL_D_CBM"]=> string(5) "1.080" [2]=> string(3) "WF2" ["AREA_CODE"]=> string(3) "WF2" ["TERR"]=> string(5) "TERR1" }
[1]=> array(7) { [0]=> string(3) "CEA" ["REF"]=> string(3) "CEA" [1]=> string(5) "2.000" ["LCL_D_CBM"]=> string(5) "2.000" [2]=> string(4) "HU13" ["AREA_CODE"]=> string(4) "HU13" ["TERR"]=> string(5) "TERR1" }
[2]=> array(7) { [0]=> string(3) "CEF" ["REF"]=> string(3) "CEA" [1]=> string(5) "2.448" ["LCL_D_CBM"]=> string(5) "2.448" [2]=> string(4) "TW16" ["AREA_CODE"]=> string(4) "TW16" ["TERR"]=> string(5) "TERR2" } ..... etc
【问题讨论】:
-
您第一次尝试实现该目标的代码是什么?
-
请用换行符重新格式化。没有人愿意滚动它。
-
@bub 请查看已编辑的问题
标签: php arrays object associative