php 三维数组转二维数组(多维数组变合拼二维数组)(foreach循环 数组叠加)

 

/***************
 * @function                         三维数组转二维数组
 * @Param:      $array :             传入参数
 * @Return:     $tempArr             返回结果数组
 ***************/
function array3_to_array2($array){
    $array = array_filter($array);
    $array = array_values($array);
    foreach ($array as $orderKey =>$orderVal){
        $count = count($orderVal);
        if($count > 1){
            for ($i = 0;$i < $count;$i++){
                $tempArr[] = $orderVal[$i];
            }
        }else{
            $tempArr[] = $orderVal[0];
        }
    }
    return $tempArr;
}

例子:三维数组

  [0] => array(19) {
    [0] => array(47) {
      ["pid"] => string(3) "291"
      ["bigclass"] => NULL
      ["title"] => string(4) "Html"
      ["class_id"] => string(1) "0"
      ["starttime"] => NULL
      ["endtime"] => NULL

    }
    [1] => array(47) {
      ["pid"] => string(3) "291"
      ["bigclass"] => string(3) "566"
      ["title"] => string(29) "HTML+css入门课程第一节"
      ["class_id"] => string(1) "0"
      ["starttime"] => string(19) "2020-07-14 09:36:36"
      ["endtime"] => string(19) "2020-07-14 11:16:36"
    }
  }
  [1] => array(14) {
    [0] => array(47) {
      ["pid"] => string(3) "128"
      ["bigclass"] => NULL
      ["title"] => string(9) "第一章"
      ["class_id"] => NULL
      ["starttime"] => NULL
      ["endtime"] => NULL
    }
    [1] => array(47) {
      ["pid"] => string(3) "128"
      ["bigclass"] => string(3) "288"
      ["title"] => string(13) "今天课程2"
      ["class_id"] => NULL
      ["starttime"] => string(19) "2019-04-18 00:00:00"
      ["endtime"] => string(19) "2019-04-18 00:30:00"
    }
    [2] => array(47) {
      ["pid"] => string(3) "128"
      ["bigclass"] => string(3) "288"
      ["title"] => string(13) "今天课程3"
      ["class_id"] => NULL
      ["starttime"] => string(19) "2019-04-22 09:57:24"
      ["endtime"] => string(19) "2019-04-22 10:27:24"
    }
  }
}

最终效果:

array(33) {
  [0] => array(47) {
    ["pid"] => string(3) "291"
    ["bigclass"] => NULL
    ["title"] => string(4) "Html"
    ["class_id"] => string(1) "0"
    ["starttime"] => NULL
    ["endtime"] => NULL
  }
  [1] => array(47) {
    ["pid"] => string(3) "291"
    ["bigclass"] => string(3) "566"
    ["title"] => string(29) "HTML+css入门课程第一节"
    ["class_id"] => string(1) "0"
    ["starttime"] => string(19) "2020-07-14 09:36:36"
    ["endtime"] => string(19) "2020-07-14 11:16:36"

  }
  [2] => array(47) {
    ["pid"] => string(3) "291"
    ["bigclass"] => string(3) "566"
    ["title"] => string(20) "HTML+css入门课程"
    ["class_id"] => string(1) "0"
    ["starttime"] => string(19) "2020-07-15 17:45:56"
    ["endtime"] => string(19) "2020-07-15 19:45:56"

  }
  [3] => array(47) {
    ["pid"] => string(3) "291"
    ["bigclass"] => string(3) "566"
    ["title"] => string(29) "HTML+css入门课程第二节"
    ["class_id"] => string(1) "0"
    ["starttime"] => string(19) "2020-07-16 09:37:21"
    ["endtime"] => string(19) "2020-07-16 11:37:21"

  }
  [4] => array(47) {
    ["pid"] => string(3) "291"
    ["bigclass"] => string(3) "566"
    ["title"] => string(32) "123HTML+css入门课程第二节"
    ["class_id"] => string(1) "0"
    ["starttime"] => string(19) "2020-07-16 14:33:15"
    ["endtime"] => string(19) "2020-07-16 17:03:15"

  }
  [5] => array(47) {
    ["pid"] => string(3) "291"
    ["bigclass"] => string(3) "566"
    ["title"] => string(31) "01HTML+css入门课程第一节"
    ["class_id"] => string(1) "0"
    ["starttime"] => string(19) "2020-07-16 17:37:18"
    ["endtime"] => string(19) "2020-07-16 19:17:18"

  }
  [6] => array(47) {
    ["pid"] => string(3) "291"
    ["bigclass"] => string(3) "566"
    ["title"] => string(32) "666HTML+css入门课程第一节"
    ["class_id"] => string(1) "0"
    ["starttime"] => string(19) "2020-07-17 09:43:25"
    ["endtime"] => string(19) "2020-07-17 12:13:25"

  }
  [7] => array(47) {
    ["pid"] => string(3) "291"
    ["bigclass"] => string(3) "566"
    ["title"] => string(31) "22HTML+css入门课程第一节"
    ["class_id"] => string(1) "0"
    ["starttime"] => string(19) "2020-07-17 14:16:34"
    ["endtime"] => string(19) "2020-07-17 17:16:34"

  }

}

 

你可能感兴趣的:(php,tp6,thinkphp5)