反向迭代数组,并从当前子数组值中减去子数组的值

时间:2018-04-27 11:18:48

标签: php arrays math multidimensional-array subtraction

我有一个像这样的动态多维数组:

$myarray = array(
            array(
                0 => 0,
                1 => 70, 
                2 => 19,
                3 => 0
            ),
            array(
                0 => 0,
                1 => 24,
                2 => 14
            ),
            array(
                0 => 0,
                1 => 15,
                2 => 11,
                3 => 0
            ),
            array(
                0 => 0,
                1 => 15,
                2 => 27,
                3 => 0
            )
        );

我需要遍历子数组并覆盖每个子数组的当前值,以表示相对于子数组键的原始值和后续值之间的差异。

这描述了每个子数组元素所需的算术(=之后的值是新的期望值):

$myarray = array(
            array(
                0 => 0 - 0 = 0
                1 => 70 - 24 = 46
                2 => 19 - 30 = -11
                3 => 0 - 0 = 0
            ),
            array(
                0 => 0 - 0 = 0
                1 => 24 - 0 = 24
                2 => 14 -(-16) = 30
                3 => 0 - 0 = 0
            ),
            array(
                0 => 0 - 0 = 0
                1 => 15 - 15 = 0
                2 => 11 - 27 = -16
                3 => 0 - 0 = 0
            ),
            array(
                0 => 0,
                1 => 15,
                2 => 27,
                3 => 0
            )
        );

我尝试做某事,但这不是我需要的,它包含错误:

$new_array = array();
foreach($myarray as $key => $values) {
    foreach($values as $k_key => $v_values) {
        $calc = $values[$k_key+1]-$v_values;
        array_push($new_array , $calc);
    }
}
var_dump($new_array);

1 个答案:

答案 0 :(得分:2)

YIKES ,我之前的尝试是以错误的顺序迭代!这是正确的方法(第一个),然后我早先的错误方式第二个。

代码:(Demo

$myarray = [
    [0, 70, 19, 0],
    [0, 24, 14],        // note missing 4th element
    [0, 15, 11, 0],
    [0, 15, 27, 0]
];

for ($i = sizeof($myarray)-2; $i>=0; --$i) {                   // start from 2nd last subarray, and move toward the start
    $max = max(sizeof($myarray[$i]), sizeof($myarray[$i+1]));  // make sure all elements are accounted for
    for($k = 0; $k < $max; ++$k) {                             // iterate and perform subtractions / declarations
        $myarray[$i][$k] = (isset($myarray[$i][$k]) ? $myarray[$i][$k] : 0) - (isset($myarray[$i+1][$k]) ? $myarray[$i+1][$k] : 0); // declare/overwrite new values as encountered
    }
}

var_export($myarray);

输出:

array (
  0 => 
  array (
    0 => 0,
    1 => 46,
    2 => -11,
    3 => 0,
  ),
  1 => 
  array (
    0 => 0,
    1 => 24,
    2 => 30,
    3 => 0,
  ),
  2 => 
  array (
    0 => 0,
    1 => 0,
    2 => -16,
    3 => 0,
  ),
  3 => 
  array (
    0 => 0,
    1 => 15,
    2 => 27,
    3 => 0,
  ),
)

由于您需要考虑可变大小的子阵列,请检查您需要多少次迭代。在尝试访问/使用它之前,基本上都使用了isset来确保元素存在。

(错误的方法从这里开始。我会留下它以防万一有人想从数组的前面开始做类似的事情。)

代码:(Demo

foreach($myarray as $k => $values) {
    if (isset($myarray[$k+1])) {
        $max = max(sizeof($values), sizeof($myarray[$k+1]));
        for($i = 0; $i < $max; ++$i) {
            $result[$k][$i] = (isset($values[$i]) ? $values[$i] : 0) - (isset($myarray[$k+1][$i]) ? $myarray[$k+1][$i] : 0);
         }
    } else {
        $result[$k] = $values;
    }
}
var_export($result);

输出:

array (
  0 => 
  array (
    0 => 0,
    1 => 46,
    2 => 5,
    3 => 0,
  ),
  1 => 
  array (
    0 => 0,
    1 => 9,
    2 => 3,
    3 => 0,
  ),
  2 => 
  array (
    0 => 0,
    1 => 0,
    2 => -16,
    3 => 0,
  ),
  3 => 
  array (
    0 => 0,
    1 => 15,
    2 => 27,
    3 => 0,
  ),
)