An array algorithm problem, three-dimensional array, how to exclude the first layer of each array inside the sub-array inside each element of the sk_id continuous 8 empty array removed?

Source: Internet
Author: User
If the subarray inside a large array has 8 consecutive sk_id of sub-elements inside each element, the sub-array is removed. The data is printed as follows:

Array (+) {[0] =array (+) {[0] = = Array (8) {["Email"] + string (+) "13537951319@163.com" ["sno"] = = string (106725097310815) "[" class_id "] + string (3)" 559 "[" user_id " ] = = string (4) "6648" ["date_time"] + string (Ten) "1473159600" ["ClassName"] + string (15)            "Oh hehe" ["sk_id"] = NULL ["num"] = + int (0)} [1] = = Array (8) { ["Email"] = + string (+) "13537951319@163.com" ["Sno"] + string ("106725097310815" ["Clas s_id "] + string (3)" 559 "[" user_id "] = + string (4)" 6648 "[" date_time "] = + string (10)" 1473          150900 "[" ClassName "] + string (15)" Oh hehe "[" sk_id "] = NULL [" num "] + int (1) } [2] = = Array (8) {["email"] + = string "13537951319@163.com" ["Sno"] =& Gt String (15) "106725097310815 "[" class_id "] + = string (3)" 559 "[" user_id "] + string (4)" 6648 "[" Date_tim            E "] = string (Ten)" 1473147900 "[" ClassName "] + string (15)" Oh hehe "[" sk_id "] = NULL ["num"] = + int (2)} [3] = = Array (8) {["email"] + = string (19) "13537951319@163. com "[" sno "] + = string (106725097310815)" ["class_id"] + string (3) "559" ["User _id "] + string (4)" 6648 "[" date_time "] + string (Ten)" 1473144600 "[" classname "] = = String (          15) "Oh hehe" ["sk_id"] + = NULL ["num"] + int (3)} .....}, [1] =array (+) {[0] = = Array (8) {["E-mail"] = + string (+) "13537951319@163.com" ["s No "] = string (3)" 106725097310815 "[" class_id "] and" 559 "[" user_id "] + string ( 4) "6648" ["DAte_time "] = string (Ten)" 1473159600 "[" ClassName "] and [] + string (15)" Oh hehe "[" sk_id "] = NULL ["num"] + int (0)} [1] = = Array (8) {["email"] + = string (19) "135379513            19@163.com "[" Sno "] and" 106725097310815 "[" class_id "] + string (3)" 559 " ["user_id"] = + string (4) "6648" ["date_time"] = = string (Ten) "1473150900" ["classname"] + = String (15) "Oh hehe" ["sk_id"] = NULL ["num"] = + int (1)} [2] = = Array (8)            {["email] + = string (+)" 13537951319@163.com "[" sno "] + + string (15)" 106725097310815 " ["class_id"] = + string (3) "559" ["user_id"] = + string (4) "6648" ["date_time"] = Strin G (Ten) "1473147900" ["ClassName"] and string (15) "Oh hehe" ["sk_id"] = NULL ["num"] =& Gt   Int (2)}       [3] = = Array (8) {["email"] + = string (+) "13537951319@163.com" ["sno"] + = string (15            ) "106725097310815" ["class_id"] + string (3) "559" ["user_id"] + string (4) "6648" ["Date_time"] + string (Ten) "1473144600" ["ClassName"] and "["] + string (15) "Oh hehe" ["sk_id"] =            NULL ["num"] = + int (3)} ....} ....}

Reply content:

If the subarray inside a large array has 8 consecutive sk_id of sub-elements inside each element, the sub-array is removed. The data is printed as follows:

Array (+) {[0] =array (+) {[0] = = Array (8) {["Email"] + string (+) "13537951319@163.com" ["sno"] = = string (106725097310815) "[" class_id "] + string (3)" 559 "[" user_id " ] = = string (4) "6648" ["date_time"] + string (Ten) "1473159600" ["ClassName"] + string (15)            "Oh hehe" ["sk_id"] = NULL ["num"] = + int (0)} [1] = = Array (8) { ["Email"] = + string (+) "13537951319@163.com" ["Sno"] + string ("106725097310815" ["Clas s_id "] + string (3)" 559 "[" user_id "] = + string (4)" 6648 "[" date_time "] = + string (10)" 1473          150900 "[" ClassName "] + string (15)" Oh hehe "[" sk_id "] = NULL [" num "] + int (1) } [2] = = Array (8) {["email"] + = string "13537951319@163.com" ["Sno"] =& Gt String (15) "106725097310815 "[" class_id "] + = string (3)" 559 "[" user_id "] + string (4)" 6648 "[" Date_tim            E "] = string (Ten)" 1473147900 "[" ClassName "] + string (15)" Oh hehe "[" sk_id "] = NULL ["num"] = + int (2)} [3] = = Array (8) {["email"] + = string (19) "13537951319@163. com "[" sno "] + = string (106725097310815)" ["class_id"] + string (3) "559" ["User _id "] + string (4)" 6648 "[" date_time "] + string (Ten)" 1473144600 "[" classname "] = = String (          15) "Oh hehe" ["sk_id"] + = NULL ["num"] + int (3)} .....}, [1] =array (+) {[0] = = Array (8) {["E-mail"] = + string (+) "13537951319@163.com" ["s No "] = string (3)" 106725097310815 "[" class_id "] and" 559 "[" user_id "] + string ( 4) "6648" ["DAte_time "] = string (Ten)" 1473159600 "[" ClassName "] and [] + string (15)" Oh hehe "[" sk_id "] = NULL ["num"] + int (0)} [1] = = Array (8) {["email"] + = string (19) "135379513            19@163.com "[" Sno "] and" 106725097310815 "[" class_id "] + string (3)" 559 " ["user_id"] = + string (4) "6648" ["date_time"] = = string (Ten) "1473150900" ["classname"] + = String (15) "Oh hehe" ["sk_id"] = NULL ["num"] = + int (1)} [2] = = Array (8)            {["email] + = string (+)" 13537951319@163.com "[" sno "] + + string (15)" 106725097310815 " ["class_id"] = + string (3) "559" ["user_id"] = + string (4) "6648" ["date_time"] = Strin G (Ten) "1473147900" ["ClassName"] and string (15) "Oh hehe" ["sk_id"] = NULL ["num"] =& Gt   Int (2)}       [3] = = Array (8) {["email"] + = string (+) "13537951319@163.com" ["sno"] + = string (15            ) "106725097310815" ["class_id"] + string (3) "559" ["user_id"] + string (4) "6648" ["Date_time"] + string (Ten) "1473144600" ["ClassName"] and "["] + string (15) "Oh hehe" ["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);

Can you look at that?

Write a simple small example, repeat three consecutive, remove the sub-array.

Array:

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:

$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);}

Output:

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

You look at this idea, I didn't do the test.

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;         }           }}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.