首頁  >  文章  >  後端開發  >  一個數組演算法問題,三維數組,怎麼排除掉在第一層的每個數組裡面的子數組裡面每個元素的sk_id連續8條為空的數組去除掉?

一個數組演算法問題,三維數組,怎麼排除掉在第一層的每個數組裡面的子數組裡面每個元素的sk_id連續8條為空的數組去除掉?

WBOY
WBOY原創
2016-09-08 08:43:511130瀏覽

如果,大數組裡面的子數組每個元素裡面有連續8個子元素的sk_id為空的則去掉這子數組。資料列印後如下:

<code>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)
          }
          ....
      }
      
      ....
}</code>

回覆內容:

如果,大數組裡面的子數組每個元素裡面有連續8個子元素的sk_id為空的則去掉這子數組。資料列印後如下:

<code>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)
          }
          ....
      }
      
      ....
}</code>

<code>   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);
</code>

你看下這樣可以嗎

寫了一個簡單的小例子,連續重複三個,去掉子數組。

數組:

<code class="php">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)
    }
  }
}
</code>

代碼:

<code class="php">$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);
}</code>

輸出:

<code class="php">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)
    }
  }
}</code>

你看看這個思路,我沒有做測試

<code>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;
    }</code>

<code>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;
         }       
    }
}</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn