-1

我需要删除所有具有 0 值的月份,只有当所有月份都匹配 0 时才必须删除它。

这是我的数组,

Array
(
    [0] => Array
        (
            [0] => Month/Year
            [1] => BRO002
            [2] => SPI001
            [3] => TYN001
        )

    [1] => Array
        (
            [0] => Jan
            [1] => 0
            [2] => 0
            [3] => 1
        )

    [2] => Array
        (
            [0] => Feb
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [3] => Array
        (
            [0] => Mar
            [1] => 0
            [2] => 0
            [3] => 0
        )

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

    [5] => Array
        (
            [0] => May
            [1] => 0
            [2] => 3
            [3] => 0
        )

    [6] => Array
        (
            [0] => Jun
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [7] => Array
        (
            [0] => Jul
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [8] => Array
        (
            [0] => Aug
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [9] => Array
        (
            [0] => Sep
            [1] => 11
            [2] => 1
            [3] => 2
        )

    [10] => Array
        (
            [0] => Oct
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [11] => Array
        (
            [0] => Nov
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [12] => Array
        (
            [0] => Dec
            [1] => 0
            [2] => 0
            [3] => 0
        )

)

上面的数组将变成以下,

Array
(
    [0] => Array
        (
            [0] => Month/Year
            [1] => BRO002
            [2] => SPI001
            [3] => TYN001
        )

    [1] => Array
        (
            [0] => Jan
            [1] => 0
            [2] => 0
            [3] => 1
        )

    [5] => Array
        (
            [0] => May
            [1] => 0
            [2] => 3
            [3] => 0
        )

    [9] => Array
        (
            [0] => Sep
            [1] => 11
            [2] => 1
            [3] => 2
        )

)

谢谢你。

4

4 回答 4

2

检查每个值:

$newArray = array();
foreach($array as $key => $val){
    if(!(!$val[1] && !$val[2] && !$val[3])) {
        $newArray[$key] = $array[$key];
    }
}

print_r($newArray);
于 2012-09-18T14:50:24.827 回答
0
$Temp = array();

foreach ($Array as $Key=>$Value) 
  if ($Value[1]!=0 || $Value[2]!=0 || $Value[3]!=0)
    $Temp[$Key] = $Value;

$Array = $Temp;
于 2012-09-18T14:50:45.360 回答
0
$array = array(
    array('Jan', 0, 0, 1), 
    array('Feb', 0, 1, 0), 
    array('Mar', 0, 0, 0), 
    array('Apr', 1, 1, 1)
);

foreach($array as $i => $a) {
    $save = false;
    foreach($a as $k => $v) {
        if($k == 0) continue; 
        if($v != 0) $save = true; 
    }
    if(!$save) unset($array[$i]);
}

var_dump(array_values($array));
于 2012-09-18T14:59:01.207 回答
0

不需要第二个数据结构:

foreach ( $data as $i => $month ) {
  if ( $i == 0 ) { continue; }
  if (
    $month[1] == 0 &&
    $month[2] == 0 &&
    $month[3] == 0 
  ) {
    unset($data[$i]);
  } 
} 
于 2012-09-18T14:59:22.610 回答