PHP多维数组求和/擦除问题

时间:2009-11-01 06:21:15

标签: php arrays multidimensional-array

我有一个如下所示的数组:

格式:

[Person#] => Array
    (
        [Bank#] => Balance
        .
        .
        [Bank#] => Balance
    )

数组:

[1] => Array
    (
        [0] => 707  //Person #1 has 707 balance in Bank #0
        [1] => 472  //Person #1 has 472 balance in Bank #1 
    )
[2] => Array
    (
        [0] => 2614
        [3] => 140
        [1] => 2802
        [4] => 245
        [2] => 0    //Person #2 has 0 balance in Bank #2
    )

[3] => Array
    (
        [2] => 0
        [3] => 0
        [0] => 1710
        [4] => 0
        [1] => 575
    )

[4] => Array
    (
        [1] => 1105
        [0] => 1010
        [4] => 0
        [3] => 120
        [2] => 0
    )

[5] => Array
    (
        [1] => 238
        [4] => 0
        [0] => 0
    )

[6] => Array
    (
        [0] => 850
        [1] => 0
    )

[7] => Array
    (
        [4] => 500
        [0] => 3397
        [1] => 2837
    )

单词“Array”左侧的数字代表一个人。第一个数字代表一个银行。第二个数字代表银行的余额。

我将这些数字打印在表格中,正如您在本例中所见,银行#2对拥有银行#2帐户的所有人都有零余额。我需要一种方法从数组中删除Bank#2 - 和/或重新创建没有Bank#2的数组。当然,它并不总是需要被移除的Bank#2,所以它必须是一个解决方案,找到总余额为零的银行(所有人)并将其删除。

或者,我可以在打印出表格之前从阵列中删除所有零余额。

我不确定哪个更容易,因为我找不到一个简单的方法来做任何一个。

3 个答案:

答案 0 :(得分:1)

删除所有余额确实非常简单:

$array = array_map('array_filter', $array);

答案 1 :(得分:1)

仅删除未使用的银行绝对不仅仅是删除所有零余额。这是我试图消除银行:

for ($bank = 0; ; ++$bank) {
    $bankUsed = null;

    foreach ($balances as $customer) {
        if (isset($customer[$bank])) {
            $bankUsed = false;

            if ($customer[$bank] > 0) {
                $bankUsed = true;
                break;
            }
        }
    }

    if ($bankUsed === null) {
        echo "Bank $bank not found. Exiting.\n";
        break;
    }
    else if ($bankUsed === false) {
        echo "Bank $bank unused.\n";

        foreach ($balances as &$customer) {
            unset($customer[$bank]);
        }
    }
}

输出:

Bank 2 unused.
Bank 5 not found. Exiting.
Array
(
    [1] => Array
        (
            [0] => 707
            [1] => 472
        )

    [2] => Array
        (
            [0] => 2614
            [3] => 140
            [1] => 2802
            [4] => 245
        )

    [3] => Array
        (
            [3] => 0
            [0] => 1710
            [4] => 0
            [1] => 575
        )

    [4] => Array
        (
            [1] => 1105
            [0] => 1010
            [4] => 0
            [3] => 120
        )

    [5] => Array
        (
            [1] => 238
            [4] => 0
            [0] => 0
        )

    [6] => Array
        (
            [0] => 850
            [1] => 0
        )

    [7] => Array
        (
            [0] => 850
            [1] => 0
        )

)

答案 2 :(得分:1)

试试这个:

<?php
error_reporting(E_ALL | E_STRICT);

header('Content-Type: text/plain');

// subset of initial data
$persons = array(
  '1' => array(0 => 707, 1 => 472),
  '2' => array(0 => 2614, 3 => 140, 1 => 2802, 4 => 245, 2 => 0),
  '3' => array(2 => 0, 3 => 0, 0 => 1710, 4 => 0, 1 => 575),
);

// build a table of bank => balances
$banks = array();
foreach ($persons as $person => $balances) {
  foreach ($balances as $bank => $balance) {
    $banks[$bank][$balance] = true;
  }
}

// remove 0 balances from the balances of each bank
// if the balances array for that bank is then empty
// then only 0 balances were there and we can remove it
$can_remove = array();
foreach ($banks as $bank => $balances) {
  unset($balances[0]);
  if (count($balances) == 0) {
    $can_remove[] = $bank;
  }
}

// go through and remove the banks that can
// be removed. Note: we have to traverse the
// array by *reference* because otherwise you
// are modifying a copy of the array (which is
// then discarded) instead of the actual array.
foreach ($persons as $person => &$balances) {
  foreach ($can_remove as $bank) {
    unset($balances[$bank]);
  }
}
print_r($persons);
?>