当前位置:Gxlcms > PHP教程 > 一个数组算法问题,三维数组,怎么排除掉在第一层的每个数组里面的子数组里面每个元素的sk_id连续8条为空的数组去除掉?

一个数组算法问题,三维数组,怎么排除掉在第一层的每个数组里面的子数组里面每个元素的sk_id连续8条为空的数组去除掉?

时间:2021-07-01 10:21:17 帮助过:5人阅读

如果,大数组里面的子数组每个元素里面有连续8个子元素的sk_id为空的则去掉这子数组。数据打印后如下:

array(100) {  

    [0] =array(88){
         [0] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473159600"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(0)
          }
          [1] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473150900"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(1)
          }
          [2] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473147900"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(2)
          }
          [3] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473144600"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(3)
          }
        
          ....
     },
      
    [1] =array(88){
         [0] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473159600"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(0)
          }
          [1] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473150900"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(1)
          }
          [2] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473147900"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(2)
          }
          [3] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473144600"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(3)
          }
          ....
      }
      
      ....
}

回复内容:

如果,大数组里面的子数组每个元素里面有连续8个子元素的sk_id为空的则去掉这子数组。数据打印后如下:

array(100) {  

    [0] =array(88){
         [0] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473159600"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(0)
          }
          [1] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473150900"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(1)
          }
          [2] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473147900"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(2)
          }
          [3] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473144600"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(3)
          }
        
          ....
     },
      
    [1] =array(88){
         [0] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473159600"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(0)
          }
          [1] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473150900"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(1)
          }
          [2] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473147900"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(2)
          }
          [3] => array(8) {
            ["email"] => string(19) "13537951319@163.com"
            ["sno"] => string(15) "106725097310815"
            ["class_id"] => string(3) "559"
            ["user_id"] => string(4) "6648"
            ["date_time"] => string(10) "1473144600"
            ["classname"] => string(15) "呵呵呵呵呵"
            ["sk_id"] => NULL
            ["num"] => int(3)
          }
          ....
      }
      
      ....
}

   foreach($arr as $k1 =>$v1){
   
       $z=0;
       foreach($v1 as $k2 =>$v2){
            if(empty($v2['sk_id'])){
                $z=$z+1;
            }else{
                $z=0;
            }
            if($z==8){
                unset($arr[$k1]);
                break;
            }
       }
        
  }


print_r($arr);

你看下这样可以吗

写了一个简单的小例子,连续重复三个,去掉子数组。

数组:

array(2) {
  [0]=>
  array(3) {
    [0]=>
    array(2) {
      ["key"]=>
      int(0)
      ["sk_id"]=>
      NULL
    }
    [1]=>
    array(2) {
      ["key"]=>
      int(1)
      ["sk_id"]=>
      NULL
    }
    [2]=>
    array(2) {
      ["key"]=>
      int(2)
      ["sk_id"]=>
      NULL
    }
  }
  [1]=>
  array(3) {
    [0]=>
    array(2) {
      ["key"]=>
      int(0)
      ["sk_id"]=>
      NULL
    }
    [1]=>
    array(2) {
      ["key"]=>
      int(1)
      ["sk_id"]=>
      NULL
    }
    [2]=>
    array(2) {
      ["key"]=>
      int(2)
      ["sk_id"]=>
      int(1)
    }
  }
}

代码:

$three = array(
        array(
            array(
                'key' => 0,
                'sk_id' => null
            ),
            array(
                'key' => 1,
                'sk_id' => null
            ),
            array(
                'key' => 2,
                'sk_id' => null
            ),
        ),
        array(
            array(
                'key' => 0,
                'sk_id' => null
            ),
            array(
                'key' => 1,
                'sk_id' => null
            ),
            array(
                'key' => 2,
                'sk_id' => 1
            ),
        ),

    );

    $result = array();
    foreach($three as $key=>$value) {
        $flag = 0;
        foreach($value as $k => $v) {
            if($v['sk_id'] == null) {
                $flag += 1;
            }
        }
        if($flag < 3) {
            array_push($result,$value);
        }
    }
    var_dump($result);
}

输出:

array(1) {
  [0]=>
  array(3) {
    [0]=>
    array(2) {
      ["key"]=>
      int(0)
      ["sk_id"]=>
      NULL
    }
    [1]=>
    array(2) {
      ["key"]=>
      int(1)
      ["sk_id"]=>
      NULL
    }
    [2]=>
    array(2) {
      ["key"]=>
      int(2)
      ["sk_id"]=>
      int(1)
    }
  }
}

你看看这个思路,我没有做测试

function test($array)
    {
        $result = array();
        foreach ($array as $key => $val) {
            $flag = false;
            foreach ($val as $k => $v) {
                if ($v['sk_id'] != null) {
                    $flag = true;
                }
            }
            if ($flag) {
                $result[] = $val;
            }
        }
        return $result;
    }

foreach ($data $key=>$row){

    $i=1;
    foreach ($row as $k=>$r){
         if (empty($r['sk_id'])){       
            $i++;
         }   
         if (!empty($r['sk_id']) && $i<8){
            $i=1;
         }      
         if ($i>8){
             unset($data[$key]);
             break;
         }       
    }
}

人气教程排行