-1

我有以下二维数组:

 Array ( [0] =>
             Array ( [0] => 20130202 
                     [1] => unknown 
                     [2] => 0 
                     [3] => 1 
                     [4] => 0 
                     [5] => 0 
                     [6] => 0 
                     [7] => 19022 
                     [8] => 0 
                     [9] => 0 ) 
          [1] => 
              Array ([0] => 20130202 
                     [1] => ZZ 
                     [2] => 1 
                     [3] => 0 
                     [4] => 0 
                     [5] => 0 
                     [6] => 0 
                     [7] => 4 
                     [8] => 0 
                     [9] => 0 ) 
                  *
                  *

我尝试删除内部数组上的特定以下条目:2、4、6、7、8、9。我正在使用的代码如下:

     foreach ($array_with_values as $key => $obj) {
        if ($key == 2 || $key == 4 || $key == 6 || $key == 7 || $key == 8 || $key == 9){
            unset($obj[$key]);
        }
      }

但它不起作用。有任何想法吗?

4

7 回答 7

1

你需要做的是...unset($array_with_values[$key])

考虑以下代码块...

$arr = array(1,2,3,4,6);

foreach($arr as $key => $a)
{
    if($a == 2)
    unset($a);
}

print_r($arr);

foreach($arr as $key => $a)
{
    if($a == 2)
    unset($arr[$key]);
}

print_r($arr);

我希望你明白我想解释。

于 2013-02-05T11:28:22.617 回答
1

您可以过滤您想要的内容,而不是关注您想要删除的内容:

$keys = array_flip([0, 1, 3, 5]);

foreach ($outer as &$inner) {
    $inner = array_intersect_key($inner, $keys);
}
于 2013-02-05T11:26:30.793 回答
1

尝试这个:

foreach ($array_with_values as $key => $obj) {
        if ($key == 2 || $key == 4 || $key == 6 || $key == 7 || $key == 8 || $key == 9){
            unset($array_with_values[$key]);
        }
      }
于 2013-02-05T11:27:38.233 回答
1

我会这样做:

$unwanted = array(2, 4, 6, 7, 8, 9);

for ($i = 0; $i < sizeof($outerArray); $i++) {
    for ($j = 0; $j < sizeof($unwanted); $j++) {
        unset($outerArray[$i][$unwanted[$j]]);
    }
}
于 2013-02-05T11:30:36.960 回答
1

只用这个unset($array_with_values[$key]);

 foreach ($array_with_values as $key => $obj) {
    if ($key == 2 || $key == 4 || $key == 6 || $key == 7 || $key == 8 || $key == 9){
        unset($array_with_values[$key]);
    }
  }
于 2013-02-05T11:21:57.913 回答
0
 foreach ($array_with_values as $key => $obj) {
    if ($key == 2 || $key == 4 || $key == 6 || $key == 7 || $key == 8 || $key == 9){
        unset($obj[$key]);
    }
  }

在这里,您的第一个数组的键将是 0,1,因此请执行以下操作:

 $delete = array (2,4,6,7,8,9);
 foreach ($array_with_values as $key => $obj) {
    foreach($delete as $val){
        unset($obj[$val]);
    }
  }
于 2013-02-05T11:23:37.133 回答
0

尝试这个

$big_array = array(

 array(20130202 ,'unknown',0,1,0,0,0,19022,0,0),
 array(20130202 ,'ZZ',1,0,0,0,0,4,0,0)
 );

 foreach ($big_array as $ikey => $array) {


       foreach ($array as $key => $val) {

          if (in_array($key, array(2,4,6,7,8,9))) {
             unset($big_array[$ikey][$key]);
          }
       }
    }

print_r($big_array);
于 2013-02-05T11:24:48.833 回答