Heim  >  Artikel  >  Backend-Entwicklung  >  Ein Array-Arithmetikproblem, ein dreidimensionales Array. Wie kann man acht aufeinanderfolgende leere Arrays mit sk_id jedes Elements im Unterarray in jedem Array auf der ersten Ebene eliminieren?

Ein Array-Arithmetikproblem, ein dreidimensionales Array. Wie kann man acht aufeinanderfolgende leere Arrays mit sk_id jedes Elements im Unterarray in jedem Array auf der ersten Ebene eliminieren?

WBOY
WBOYOriginal
2016-09-08 08:43:511130Durchsuche

Wenn jedes Element des Unterarrays im großen Array 8 aufeinanderfolgende Unterelemente hat, deren sk_id leer ist, dann entfernen Sie dieses Unterarray. Die Daten werden wie folgt gedruckt:

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

Antwortinhalt:

Wenn jedes Element des Unterarrays im großen Array 8 aufeinanderfolgende Unterelemente hat, deren sk_id leer ist, dann entfernen Sie dieses Unterarray. Die Daten werden wie folgt gedruckt:

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

Können Sie sehen, ob das in Ordnung ist?

Schreiben Sie ein einfaches Beispiel, wiederholen Sie es dreimal hintereinander und entfernen Sie das Subarray.

Array:

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

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

Ausgabe:

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

Sehen Sie sich diese Idee an, ich habe sie nicht getestet

<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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn