I have a php script creating a multidimensional array:
$res = mysql_query("SELECT `date`, `temperature` FROM `general` ORDER BY `date`", $db);
$outside_temperature_array = array();
while($row = mysql_fetch_assoc($res)) {
$date = $row['date'];
$temperature = $row['temperature'];
$temp_array = array();
array_push($temp_array, $date);
array_push($temp_array, $temperature);
array_push($outside_temperature_array, $temp_array);
unset($temp_array);
}
print_r($outside_temperature_array);
The multidimensional array looks like this. It has a unix timestamp in sequential order along with a value.
Array
(
[0] => Array
(
[0] => 1452483001
[1] => 40
)
[1] => Array
(
[0] => 1452483301
[1] => 39
)
[2] => Array
(
[0] => 1452483600
[1] => 39
)
[3] => Array
(
[0] => 1452483901
[1] => 39
)
[4] => Array
(
[0] => 1452484201
[1] => 39
)
[5] => Array
(
[0] => 1452484502
[1] => 39
)
[6] => Array
(
[0] => 1452484801
[1] => 38
)
[7] => Array
(
[0] => 1452485101
[1] => 38
)
[8] => Array
(
[0] => 1452485400
[1] => 38
)
[9] => Array
(
[0] => 1452485701
[1] => 39
)
[10] => Array
(
[0] => 1452486002
[1] => 39
)
)
I want to omit all identical values except for the first and last, only when they show up sequentially. Think of this plotted on a line graph. I basically want to remove the unnecessary values that fall between two points of identical values. So the above array would change to this:
Array
(
[0] => Array
(
[0] => 1452483001
[1] => 40
)
[1] => Array
(
[0] => 1452483301
[1] => 39
)
[2] => Array
(
[0] => 1452484502
[1] => 39
)
[3] => Array
(
[0] => 1452484801
[1] => 38
)
[4] => Array
(
[0] => 1452485400
[1] => 38
)
[5] => Array
(
[0] => 1452485701
[1] => 39
)
[6] => Array
(
[0] => 1452486002
[1] => 39
)
)
You need to:
This should work for you.
<?php
$res = mysql_query("SELECT `date`, `temperature` FROM `general` ORDER BY `date`", $db);
$outside_temperature_array = array();
$last_temp = null;
$held_row = null;
while($row = mysql_fetch_assoc($res)) {
// If we're on a new temperature
if ($row['temperature'] !== $last_temp) {
// Append and clear the held row first
if (is_array($held_row)) {
$outside_temperature_array[] = [$held_row['date'], $held_row['temperature']];
$held_row = null;
}
// Append this row and note the temperature
$outside_temperature_array[] = array($row['date'], $row['temperature']);
$last_temp = $row['temperature'];
} else {
// Hold the row in case the next row is different
$held_row = $row;
}
}
// If the last row was not appended to the array
if (is_array($held_row)) {
// Append the held row
$outside_temperature_array[] = array($held_row['date'], $held_row['temperature']);
}
print_r($outside_temperature_array);
$res = mysql_query("SELECT `date`, `temperature` FROM `general` ORDER BY `date`", $db);
$outside_temperature_array = array();
$oldTemperature = null;
while($row = mysql_fetch_assoc($res)) {
if($oldTemperature == $row['temperature'] && next($row)['temperature'] == $oldTemperature )
continue;
}
$oldTemperature = $row['temperature'];
$outside_temperature_array[] = array($row['date'],$row['temperature']);
}
print_r($outside_temperature_array);
You can try passed eqvivalent element in array use continue, but this code non testing. I think maybe you can execute sql query for this. And mysql this old extensions you need use PDO or Mysqli.