技术问题总结

2019-03-27 18:14:55   PHP

  php  

1.写个函数,将数组array(0,1,2,array(3,4,array(5),6),7,array(8,array(9)),array(10,array(11,array(12))),...);转换为新数组array(0,1,2,3,4,5,6,7,8,9,10,11,12,...)

解答:$arr = array(0,1,2,array(3,4,array(5),6),7,array(8,array(9)),array(10,array(11,array(12))));
    $data = [];
    function arrs($arr){
        global $data;
        foreach($arr as $k=>$v){
            if(is_array($v)){
                arrs($v);
            }else{
                array_push($data,$v);
            }
        }
    }

    arrs($arr);
    print_r($data);

    解析:利用递归思想和数组函数
2.无限分类树状结构
$data = [
        0 => [
            'id' => 1,
            'parent_id' => 0,
            'name' => '发动机系统'
            ],
        1 => [
            'id' => 2,
            'parent_id' => 1,
            'name' => '机体组'
            ],
        2 => [
            'id' => 3,
            'parent_id' => 2,
            'name' => '发动机总成'
            ],
        3 => [
            'id' => 4,
            'parent_id' => 2,
            'name' => '发动机置悬'
            ],
        4 => [
            'id' => 5,
            'parent_id' => 1,
            'name' => '配气系统'
            ],
        5 => [
            'id' => 6,
            'parent_id' => 4,
            'name' => '气门'
            ],
        6 => [
            'id' => 7,
            'parent_id' => 0,
            'name' => '行走系统'
            ],
        7 => [
            'id' => 8,
            'parent_id' => 7,
            'name' => '前桥'
            ],
        8 => [
            'id' => 9,
            'parent_id' => 7,
            'name' => '半轴'
            ],
        9 => [
            'id' => 10,
            'parent_id' => 5,
            'name' => '气栓'
            ],
        10 => [
            'id' => 11,
            'parent_id' => 4,
            'name' => '气栓'
            ],
        ];

    function array_to_tree($data, $parent_id = 0, $option = [])
    {
        $tree = [];
        foreach ($data as $k => $item) {

            if ($item['parent_id'] == $parent_id) {
                unset($data[$k]);
                foreach ($option as $k => $v) {
                    $item[$v] = $item[$k];
                }
                $item['children'] = array_to_tree($data, $item['id'], $option);
                $tree[] = $item;

                $tree = array_merge($tree);

            }
        }
        return $tree;
    }

    $data = array_to_tree($data, 0, ['name' => 'label', 'id' => 'value', 'parent_id' => 'parent']);

    dd($data);
    ----------------------------------------------------------------------------------------
    array:2 [
  0 => array:7 [
    "id" => 1
    "parent_id" => 0
    "name" => "发动机系统"
    "label" => "发动机系统"
    "value" => 1
    "parent" => 0
    "children" => array:2 [
      0 => array:7 [
        "id" => 2
        "parent_id" => 1
        "name" => "机体组"
        "label" => "机体组"
        "value" => 2
        "parent" => 1
        "children" => array:2 [
          0 => array:7 [
            "id" => 3
            "parent_id" => 2
            "name" => "发动机总成"
            "label" => "发动机总成"
            "value" => 3
            "parent" => 2
            "children" => []
          ]
          1 => array:7 [
            "id" => 4
            "parent_id" => 2
            "name" => "发动机置悬"
            "label" => "发动机置悬"
            "value" => 4
            "parent" => 2
            "children" => array:2 [
              0 => array:7 [
                "id" => 6
                "parent_id" => 4
                "name" => "气门"
                "label" => "气门"
                "value" => 6
                "parent" => 4
                "children" => []
              ]
              1 => array:7 [
                "id" => 11
                "parent_id" => 4
                "name" => "气栓"
                "label" => "气栓"
                "value" => 11
                "parent" => 4
                "children" => []
              ]
            ]
          ]
        ]
      ]
      1 => array:7 [
        "id" => 5
        "parent_id" => 1
        "name" => "配气系统"
        "label" => "配气系统"
        "value" => 5
        "parent" => 1
        "children" => array:1 [
          0 => array:7 [
            "id" => 10
            "parent_id" => 5
            "name" => "气栓"
            "label" => "气栓"
            "value" => 10
            "parent" => 5
            "children" => []
          ]
        ]
      ]
    ]
  ]
  1 => array:7 [
    "id" => 7
    "parent_id" => 0
    "name" => "行走系统"
    "label" => "行走系统"
    "value" => 7
    "parent" => 0
    "children" => array:2 [
      0 => array:7 [
        "id" => 8
        "parent_id" => 7
        "name" => "前桥"
        "label" => "前桥"
        "value" => 8
        "parent" => 7
        "children" => []
      ]
      1 => array:7 [
        "id" => 9
        "parent_id" => 7
        "name" => "半轴"
        "label" => "半轴"
        "value" => 9
        "parent" => 7
        "children" => []
      ]
    ]
  ]
]
3.数据库
student (id,name.age.,sex) 学生表
course (id,name,teacher_id) 课程表
score (student_id,course_id score) 分数表
teacher (id,name) 教师表

a,查询姓"黄"的老师的个数
select count(*) from teacher where name like '黄%';

b.查询同时学过语文和数学的同学的编号,姓名
select score.student_id as stu_id ,student.name from score 
join course on score.course_id=course.id
join student on score.student_id=student.id
where course.name = '语文' or course.name = '数学' 
GROUP BY stu_id HAVING count(*)=2;

c.查询所有课程成绩大于60分的同学的编号,姓名
select id,name from (select min(score) as ms, student_id from score group by student_id) result join student on result.student_id=student.id where ms > 60

d.查询所有课程成绩小于60分的同学的编号,姓名
select id,name from (select max(score) as ms, student_id from score group by student_id) result join student on result.student_id=student.id where ms < 60
4.面向对象编程的理解
4.api加密的机制
4.用五种方法获取三位数的最大值