Skip to content

Commit d9c7308

Browse files
committed
added nested column in sum method
1 parent 2ca052e commit d9c7308

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

examples/data.json

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,30 @@
77
"website":"https://nahid.im"
88
},
99
"users":[
10-
{"id":1, "name":"Johura Akter Sumi", "location": "dhaka"},
11-
{"id":2, "name":"Mehedi Hasan Nahid", "location": "barisal"},
12-
{"id":3, "name":"Ariful Islam", "location": "barisal"},
13-
{"id":4, "name":"Suhel Ahmed", "location": "sylhet"},
14-
{"id":5, "name":"Firoz Serniabat", "location": "barisal"},
15-
{"id":6, "name":"Musa Jewel", "location": "barisal", "visits": [
16-
{"name": "Sylhet", "year": 2011},
17-
{"name": "Cox's Bazar", "year": 2012},
18-
{"name": "Bandarbar", "year": 2014}
19-
]}
10+
{"id":1, "name":"Johura Akter Sumi", "location": "dhaka", "visits": {
11+
"name" : "Barisal",
12+
"year" : 2010
13+
}},
14+
{"id":2, "name":"Mehedi Hasan Nahid", "location": "barisal", "visits":{
15+
"name" : "Dhaka",
16+
"year" : 2015
17+
}},
18+
{"id":3, "name":"Ariful Islam", "location": "barisal", "visits": {
19+
"name" : "Dhaka",
20+
"year" : 2010
21+
}},
22+
{"id":4, "name":"Suhel Ahmed", "location": "sylhet", "visits": {
23+
"name" : "Barisal",
24+
"year" : 2014
25+
}},
26+
{"id":5, "name":"Firoz Serniabat", "location": "barisal", "visits": {
27+
"name" : "Barisal",
28+
"year" : 2011
29+
}},
30+
{"id":6, "name":"Musa Jewel", "location": "barisal", "visits": {
31+
"name" : "Khulna",
32+
"year" : 2012
33+
}}
2034
],
2135
"products": [
2236
{"id":1, "user_id": 2, "city": "bsl", "name":"iPhone", "cat":1, "price": 80000},

examples/index.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
//});
1616
//
1717

18-
$jq = new Jsonq('data1.json');
18+
$jq = new Jsonq('data.json');
1919

2020
try {
21-
$result = $jq->from('.')
22-
//->whereContains('title', 'Day')
23-
//->where('year', '>=', 2000)
24-
->countGroupBy('year')
25-
->avg();
21+
$result = $jq->from('users')
22+
->where('visits.year', '=', 2010)
23+
->sum('visits.year');
2624
dump($result);
2725
} catch (\Nahid\JsonQ\Exceptions\ConditionNotAllowedException $e) {
2826

src/Jsonq.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ public function sum($column = null)
249249
$sum = array_sum($this->_map);
250250
} else {
251251
foreach ($this->_map as $key => $val) {
252-
if (isset($val[$column])) {
253-
if (is_numeric($val[$column])) {
254-
$sum += $val[$column];
255-
}
252+
$value = $this->getFromNested($val, $column);
253+
if (is_scalar($value)) {
254+
$sum += $value;
256255
}
256+
257257
}
258258
}
259259

0 commit comments

Comments
 (0)