除第一个和最后一个之外删除相同的

时间:2016-01-22 20:44:22

标签: php arrays multidimensional-array

我有一个PHP脚本创建一个多维数组:

$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);

多维数组看起来像这样。它具有连续顺序的unix时间戳和值。

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
        )
)

我想省略所有相同的值,除了第一个和最后一个,只有当它们按顺序出现时。可以想象这是在折线图上绘制的。我基本上想要删除两个相同值之间的不必要值。所以上面的数组会改为:

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
        )
)

2 个答案:

答案 0 :(得分:0)

$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);

您可以尝试在数组中使用继续传递eqvivalent元素,但此代码不进行测试。我想也许你可以为此执行sql查询。而mysql这个旧的扩展你需要使用PDO或Mysqli。

答案 1 :(得分:0)

你需要:

  1. 跟踪最后的温度。
  2. 检测到温度变化时,将保留的行(如果适用)和当前行附加到最终数组。
  3. 如果没有温度变化,请将该行保留为最后一个副本。
  4. 循环后,如果已设置,则将保留的行追加到最终数组。
  5. 这应该适合你。

    <?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);