Good Night, i need some help with my PHP code. I have 1 big array that cointains read data from some machines. The machines ID is the row "idSensor".
I want to split this big into another arrays grouped by the row idSensor.
What i have at the moment
Array
(
[1010_0] => Array
(
[IdSensorData] => 1
[Timestamp] => 2015-06-16 18:08:23.000
[idSensor] => 1010
[IdEnergyPrice] => 1
[ReadData] => 1459
[ReadEnergyPrice] => 0
)
[1010_1] => Array
(
[IdSensorData] => 73
[Timestamp] => 2015-06-16 18:09:22.000
[idSensor] => 1010
[IdEnergyPrice] => 1
[ReadData] => 1477
[ReadEnergyPrice] => 0
)
[1011_0] => Array
(
[IdSensorData] => 2
[Timestamp] => 2015-06-16 18:08:23.000
[idSensor] => 1011
[IdEnergyPrice] => 1
[ReadData] => 1183
[ReadEnergyPrice] => 0
)
[1011_1] => Array
(
[IdSensorData] => 74
[Timestamp] => 2015-06-16 18:09:23.000
[idSensor] => 1011
[IdEnergyPrice] => 1
[ReadData] => 1307
[ReadEnergyPrice] => 0
)
)
What i want to achieve
Array 1010
[Timestamp] => 2015-06-16 18:08:23.000,[ReadData] => 1459
[Timestamp] => 2015-06-16 18:09:22.000,[ReadData] => 1477
Array 1011
[Timestamp] => 2015-06-16 18:08:23.000,[ReadData] => 1183
[Timestamp] => 2015-06-16 18:09:23.000,[ReadData] => 1307
Here is my main function csv.php
function getarray($ip,$readinterval)
{
global $db; /*variavel global pois a funcao nao a consegue ir buscar diretamnte aos includes*/
$sensores = array();/*Array para armazenar os sensores*/
$resultado = array();/*Array para armazenar os resltados da pesquisa do csv*/
/*Vamos buscar a base de dados os idSensor com o IP e intervalo de leitura que desejamos*/
$sql = "SELECT idSensor FROM Sensor where IP = '{$ip}' and ReadInterval = '{$readinterval}'" or die("Erro na query");
$result = $db->query($sql);
if ($result->num_rows > 0)
{
echo "Temos resultados para o edificio: " .$ip." e tempo de leitura: ".$readinterval. " min</br></br>";
while($row = $result->fetch_assoc())
{
$sensores[] = $row["idSensor"];
}
} else {
echo "Nao encontrou resultados";
}
$db->close();
/*Agora para cada sensor do vetor sensores vamos buscar as suas informacoes ao CSV e armazenar tudo dentro do resultado*/
foreach ($sensores as $value)
{
$csv = new parseCSV();
$csv->conditions = "idSensor is '{$value}'";
$csv->sort_by = 'idSensor';
$csv->auto('eco.csv');
$csv->parse('eco.csv');
$resultado = array_merge($resultado,$csv->data);
}
/*Apresenta os resultados todos formatados*/
print "<pre>";
print_r($resultado);
print "</pre>";
After searching a little bit more I found what I was looking for.
I made some modifications on the code and I solved my problem. Thanks all.
I think you can collect your data structure in your main loop.
I haven't tested the code, so I'm not sure. May be it will get you somewhere close.
foreach ($sensores as $value)
{
$csv = new parseCSV();
$csv->conditions = "idSensor is '{$value}'";
$csv->sort_by = 'idSensor';
$csv->auto('eco.csv');
$csv->parse('eco.csv');
$resultArray[$csv->data['idSensor']][] = array ('Timestamp' => $csv->data['Timestamp'], 'ReadData' => $csv->data['ReadData']);
$resultado = array_merge($resultado,$csv->data);
}
Try this:
$newArr = [ ];
foreach($your_data as $key => $val) {
if($key == 'idSensor') {
$newArr[$key][ ] = ['Timestamp' => $val['Timestamp'], 'ReadData => $val['ReadData']
}
}
print_r($newArr);