getdata()取得值格式
[0] => array(6) { ["id"] => string(2) "41" ["type"] => string(1) "2" ["fraction"] => string(3) "4.0" ["answer"] => string(3) "A,B" } [1] => array(6) { ["id"] => string(2) "45" ["type"] => string(1) "2" ["fraction"] => string(3) "4.0" ["answer"] => string(1) "B" } foreach($list as $k=>$val){ $data2=[]; $data2 = getdata($k); $data=array_merge($data,$data2); }合拼的数组键值是从0开始的
$data=array_merge($data,$data2);
合拼的数组键值不改变
例子:三维数组
[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" } }例子1:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$msg = [
"code" => "0",
"msg" => "",
"count" => "",
];
$data = [
"name" => "lady",
"age" => "0",
"count" => "0",
];
$msg['data'] = $data;
print_r($msg);
输出:
1
2
3
4
5
6
7
8
9
10
11
12
Array
(
[code] => 0
[msg] =>
[count] =>
[data] => Array
(
[name] => lady
[age] => 0
[count] => 0
)
)
例子二:
1
2
3
4
5
6
7
8
<?php
$a1 = [
'red', 'green',
];
$a2 = [
'blue', 'yellow',
];
print_r(array_merge($a1, $a2));
输出:
1
2
3
4
5
6
7
Array
(
[0] => red
[1] => green
[2] => blue
[3] => yellow
)
例子三:
1
2
3
4
5
6
7
8
9
10
11
<?php
$a1 = [
'apple' => 'red',
'banner' => 'yellow',
];
$a2 = [
'pear' => 'yellwo',
'apple' => 'green',
];
$result = array_merge_recursive($a1, $a2);
print_r($result);
输出:
1
2
3
4
5
6
7
8
9
10
11
Array
(
[apple] => Array
(
[0] => red
[1] => green
)
[banner] => yellow
[pear] => yellwo
)
例子四:
1
2
3
4
5
<?php
$a = array('color' => 'red');
$b = array('color' => 'blue', 'type' => 'fruit');
$arr = $a + $b;
print_r($arr);
输出:
1
2
3
4
5
Array
(
[color] => red
[type] => fruit
)
例子五:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
$a = [
[
'id' => '1',
'name' => '张三',
'age' => '18',
],
[
'id' => '1',
'name' => '小米',
'age' => '15',
],
[
'id' => '3',
'name' => '张三',
'age' => '20',
],
[
'id' => '3',
'name' => '张六',
'age' => '29',
],
[
'id' => '3',
'name' => '张三疯',
'age' => '20',
],
];
$b = [];
foreach ($a as $k => $v) {
if (!isset($b[$v['id']])) {
$b[$v['id']] = $v;
} else {
$b[$v['id']]['name'] .= ',' . $v['name'];
}
}
// echo '<pre>';
print_r(array_values($b));
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Array
(
[0] => Array
(
[id] => 1
[name] => 张三,小米
[age] => 18
)
[1] => Array
(
[id] => 3
[name] => 张三,张六,张三疯
[age] => 20
)
)
例子六:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
$arr = array(
array(
'id' => 1,
'pid' => 0,
'name' => '新闻分类',
),
array(
'id' => 2,
'pid' => 0,
'name' => '最新公告',
),
array(
'id' => 3,
'pid' => 1,
'name' => '国内新闻',
),
array(
'id' => 4,
'pid' => 1,
'name' => '国际新闻',
),
array(
'id' => 5,
'pid' => 0,
'name' => '图片分类',
),
array(
'id' => 6,
'pid' => 5,
'name' => '新闻图片',
),
array(
'id' => 7,
'pid' => 5,
'name' => '其它图片',
),
);
//整理函数
/**
* 生成无限级树算法
* @author Baiyu 2014-04-01
* @param array $arr 输入数组
* @param number $pid 根级的pid
* @param string $column_name 列名,id|pid父id的名字|children子数组的键名
* @return array $ret
*/
function make_tree($arr, $pid = 0, $column_name = 'id|pid|children') {
list($idname, $pidname, $cldname) = explode('|', $column_name);
$ret = array();
foreach ($arr as $k => $v) {
if ($v[$pidname] == $pid) {
$tmp = $arr[$k];
unset($arr[$k]);
$tmp[$cldname] = make_tree($arr, $v[$idname], $column_name);
$ret[] = $tmp;
}
}
return $ret;
}
$output_array = make_tree($arr);
print_r($output_array);
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Array
(
[0] => Array
(
[id] => 1
[pid] => 0
[name] => 新闻分类
[children] => Array
(
[0] => Array
(
[id] => 3
[pid] => 1
[name] => 国内新闻
[children] => Array
(
)
)
[1] => Array
(
[id] => 4
[pid] => 1
[name] => 国际新闻
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 2
[pid] => 0
[name] => 最新公告
[children] => Array
(
)
)
[2] => Array
(
[id] => 5
[pid] => 0
[name] => 图片分类
[children] => Array
(
[0] => Array
(
[id] => 6
[pid] => 5
[name] => 新闻图片
[children] => Array
(
)
)
[1] => Array
(
[id] => 7
[pid] => 5
[name] => 其它图片
[children] => Array
(
)
)
)
)
)