-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

Check each value:

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

2 番目のデータ構造を必要としない場合:

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 に答える
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 に答える