0
I have the following array:
Array
(
[0] => stdClass Object
(
[anomes] => 201601
[codreg] => 41
[nomreg] => PR
[codcin] => 1
[nomcin] => OUTROS
[valven] => 6835.7000
[qtdven] => 1078.8000
)
[1] => stdClass Object
(
[anomes] => 201601
[codreg] => 42
[nomreg] => SC
[codcin] => 1
[nomcin] => OUTROS
[valven] => 3129.0000
[qtdven] => 366.6200
)
[2] => stdClass Object
(
[anomes] => 201601
[codreg] => 42
[nomreg] => SC
[codcin] => 2
[nomcin] => PECAS
[valven] => 346.9100
[qtdven] => 73.6600
)
)
and would like to group it by codreg, implemented the following:
$saida = array();
foreach ($dadf513 as $row) {
if(!isset($saida[$row->nomreg])) {
$saida[$row->nomreg] = array(floatval($row->valven));
} else {
array_push($saida[$row->nomreg], floatval($row->valven));
}
$arrayFinal[$row->nomreg] = [
'name' => $row->nomreg,
'data' => $saida[$row->nomreg]
];
}
print_r($arrayFinal);
and object to the result:
Array
(
[PR] => Array
(
[name] => PR
[data] => Array
(
[0] => 6835.7
)
)
[SC] => Array
(
[name] => SC
[data] => Array
(
[0] => 3129
[1] => 346.91
)
)
)
So far beauty, my doubt is the following, how to pass the value 0 in the [date], when there is no value? To stay as follows:
Array
(
[PR] => Array
(
[name] => PR
[data] => Array
(
[0] => 6835.7
[1] => 0
)
)
[SC] => Array
(
[name] => SC
[data] => Array
(
[0] => 3129
[1] => 346.91
)
)
)
It has not been clear what the relationship of their grouping is, it seems to me that there have been more changes than just making a
map
grouping by code.– Israel Merljak
@Israelmerljak the initial array is the result of a bank select, after that I did the code below
– wribeiro
The question is not very clear. What happens if you have 3 objects with
SC
and 1PR
? What happens if you have 3SC
2PR
and another distinct ?– Isac