例子:

array(4) {
["code"] => int(200)
["msg"] => string(6) "成功"
["count"] => int(12)
["data"] => array(5) {
["data_0"] => array(12) {
[1] => string(6) "首页"
[3] => string(12) "直播课程"
[4] => string(12) "录播课程"
[5] => string(12) "在线题库"
[6] => string(6) "团队"
[7] => string(6) "资讯"
[8] => string(6) "商城"
[9] => string(6) "论坛"
[11] => string(12) "新手指南"
[12] => string(12) "帮助中心"
[18] => string(12) "在线招聘"
[14] => string(12) "关于我们"
}
["data_1"] => array(10) {
[3] => array(2) {
[26] => string(6) "教师"
[25] => string(6) "自考"
}
[4] => array(2) {
[34] => string(7) "自考1"
[33] => string(7) "教师1"
}
[5] => array(3) {
[41] => string(6) "教师"
[40] => string(6) "人力"
[39] => string(6) "自考"
}
[6] => array(3) {
[53] => string(6) "人力"
[52] => string(6) "教师"
[51] => string(6) "会计"
}
[7] => array(3) {
[59] => string(6) "公告"
[58] => string(12) "行业新闻"
[57] => string(12) "公司新闻"
}
[8] => array(2) {
[61] => string(6) "自考"
[60] => string(9) "计算机"
}
[11] => array(6) {
[70] => string(12) "交流社区"
[69] => string(12) "疑难问答"
[68] => string(9) "VIP购买"
[67] => string(12) "意见反馈"
[66] => string(15) "请修改标题"
[65] => string(15) "请修改标题"
}
[12] => array(5) {
[75] => string(12) "如何支付"
[74] => string(12) "注册账号"
[73] => string(12) "新手入门"
[72] => string(12) "发布须知"
[71] => string(15) "请修改标题"
}
[18] => array(3) {
[78] => string(15) "技术部招聘"
[77] => string(15) "市场部招聘"
[76] => string(15) "文职类招聘"
}
[14] => array(2) {
[80] => string(12) "关于我们"
[79] => string(12) "联系方式"
}
}
["data_2"] => array(12) {
[26] => array(2) {
[28] => string(6) "中学"
[27] => string(6) "小学"
}
[25] => array(2) {
[30] => string(6) "专科"
[29] => string(6) "本科"
}
[34] => array(2) {
[36] => string(6) "博士"
[35] => string(9) "研究生"
}
[33] => array(2) {
[38] => string(7) "小学2"
[37] => string(7) "中学2"
}
[41] => array(3) {
[44] => string(6) "小学"
[43] => string(6) "中学"
[42] => string(6) "高中"
}
[40] => array(2) {
[46] => string(7) "中学1"
[45] => string(7) "小学1"
}
[39] => array(2) {
[48] => string(12) "行管本科"
[47] => string(12) "行管专科"
}
[53] => array(1) {
[54] => string(6) "大专"
}
[52] => array(1) {
[55] => string(6) "大专"
}
[51] => array(1) {
[56] => string(6) "大专"
}
[61] => array(1) {
[62] => string(21) "专科升本科密级"
}
[60] => array(2) {
[64] => string(8) "JS教程"
[63] => string(9) "PHP教程"
}
}
["data_3"] => array(3) {
[28] => array(1) {
[32] => string(15) "请修改标题"
}
[30] => array(1) {
[31] => string(15) "请修改标题"
}
[44] => array(2) {
[50] => string(21) "教育知识与能力"
[49] => string(12) "综合素质"
}
}
["data_4"] => NULL
}
}

比如要取得data_1数组值3的数组

$indexmenudata['data']['data_1']['3'];

结果:

array(2) {
[26] => string(6) "教师"
[25] => string(6) "自考"
}

 

对于一个多维数组(以二维数组为例),经常需要获取到其中的一列,比如有几条用户数据,是一个二维数组,现在需要获取到这几个用户的名字,可以有多种方法实现:


$arr = array(
array(
'id'=>'101',
'name'=>'qu',
'age'=>28
),
array(
'id'=>'102',
'name'=>'you',
'age'=>24
),
array(
'id'=>'103',
'name'=>'zheng',
'age'=>22
),
array(
'id'=>'104',
'name'=>'zhu',
'age'=>23
)
);


方法一:使用PHP内置函数array_column()实现


array array_column ( array $input , mixed $column_key [, mixed $index_key ] )


执行语句:

$result = array_column($arr,'name'); 
print_r($result);


结果如下:


Array 
(
[0] => qu
[1] => you
[2] => zheng
[3] => zhu
)


如果指定了可选参数index_key,那么input数组中的这一列的值将作为返回数组中对应值的键。


$result = array_column($arr,'name','id'); 
print_r($result);


结果如下:

Array 
(
[101] => qu
[102] => you
[103] => zheng
[104] => zhu
)


方法二:使用PHP内置函数array_map()实现


array array_map ( callable $callback , array $arr1 [, array $... ] )


array_map() 返回一个数组,该数组包含了 arr1 中的所有单元经过 callback 作用过之后的单元。其中第一个参数是一个回调函数,返回值是一个数组,该数组的每个元素都数组(arr1)里面的每个元素经过回调函数(callback)处理了的。

先声明一个处理函数:

function get_val($arr){ 
return $arr['name'];
}


然后作用于array_map()函数:


$result = array_map('get_val',$arr); 
print_r($result);


执行结果如下:


Array 
(
[0] => qu
[1] => you
[2] => zheng
[3] => zhu
)


此处array_map()的第一个参数是一个回调函数,而且是事先定义好的有名函数,这里我们也可以像js一样使用匿名函数:


$result = array_map(function($v){ 
return $v['name'];
},$arr);


其执行结果是一样的。