php递归遍历多维数组,合并重复值并记录重复次数 PHP递归 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给个思路:先降维function untree($ar, $key='children', $deep=0, $paren='') { $res = array(); foreach((array)$ar as $v) { if(is_numeric(key($v))) { $res = array_merge($res, untree($v, $key, $deep+1, $paren)); continue; } $v['deep'] = $deep; $v['paren'] = $paren; if(isset($v[$key])) { $t = $v[$key]; $pa = $v['text']; unset($v[$key]); } $res[] = $v; if(! empty($t)) $res = array_merge($res, untree($t, $key, $deep+1, $pa)); } return $res;}print_r(untree($array));得Array( [0] => Array ( [text] => 体育 [deep] => 1 [paren] => ) [1] => Array ( [text] => 篮球 [grade] => 1 [deep] => 2 [paren] => 体育 ) [2] => Array ( [text] => 足球 [grade] => 3 [deep] => 2 [paren] => 体育 ) [3] => Array ( [text] => 音乐 [deep] => 1 [paren] => ) [4] => Array ( [text] => 唱歌 [deep] => 2 [paren] => 音乐 ) [5] => Array ( [text] => 儿歌三百首 [grade] => 1 [deep] => 3 [paren] => 唱歌 ) [6] => Array ( [text] => 跳舞 [grade] => 3 [deep] => 2 [paren] => 音乐 ) [7] => Array ( [text] => 儿歌三百首 [grade] => 1 [deep] => 3 [paren] => 唱歌 ) [8] => Array ( [text] => 体育 [deep] => 1 [paren] => ) [9] => Array ( [text] => 篮球 [grade] => 2 [deep] => 2 [paren] => 体育 ) [10] => Array ( [text] => 排球 [grade] => 5 [deep] => 2 [paren] => 体育 ) [11] => Array ( [text] => 音乐 [deep] => 1 [paren] => ) [12] => Array ( [text] => 唱歌 [deep] => 2 [paren] => 音乐 ) [13] => Array ( [text] => 儿歌三百首 [grade] => 4 [deep] => 3 [paren] => 唱歌 )) 其实原始数组有记录id ,pid值的 $array=array( '0' => array( '0' => array( 'id' => 87073074, 'pid' => 0, 'text' => '白田最新範疇', 'children' => array( '0' => array( 'id' => 67852256, 'pid' => 87073074, 'text' => '範疇知識', 'children' => array( '0' => array( 'id' => 44740741, 'pid' => 67852256, 'text' => '體能與健康', 'children' => array( '0' => array( 'id' => 66256396, 'pid' => 44740741, 'text' => '小肌肉', 'children' => array( '0' => array( 'id' => 71852852, 'pid' => 66256396, 'text' => '掌握雙手協調能力', 'children' => array( '0' => array( 'id' => 84741741, 'text' => '能摺錢包', 'grade' => 1 ) ) ) ) ) ) ), '1' => array( 'id' => 32518528, 'pid' => 67852256, 'text' => '語文', 'children' => array( '0' => array( 'id' => 18185185, 'pid' => 32518528, 'text' => '聽', 'children' => array( '0' => array( 'id' => 35256896, 'pid' => 18185185, 'text' => '能理解故事內容要點', 'children' => array( '0' => array( 'id' => 69295296, 'text' => '能理解故事內容要點', 'grade' => 1 ) ) ), '1' => array( 'id' => 54740741, 'pid' => 18185185, 'text' => '能\'聽\',指示進行遊戲和活動', 'children' => array( '0' => array( 'id' => 93629639, 'text' => '能\'聽\',指示進行活動和遊戲', 'grade' => 1 ) ) ) ) ) ) ) ) ) ) ) ), '1' => array( '0' => array( 'id' => 87073074, 'pid' => 0, 'text' => '白田最新範疇', 'children' => array( '0' => array( 'id' => 67852256, 'pid' => 87073074, 'text' => '範疇知識', 'children' => array( '0' => array( 'id' => 44740741, 'pid' => 67852256, 'text' => '體能與健康', 'children' => array( '0' => array( 'id' => 66256396, 'pid' => 44740741, 'text' => '小肌肉', 'children' => array( '0' => array( 'id' => 71852852, 'pid' => 66256396, 'text' => '掌握雙手協調能力', 'children' => array( '0' => array( 'id' => 84741741, 'text' => '能摺錢包', 'grade' => 2 ) ) ) ) ) ) ) ) ) ) ) )); 降维还是必须的然后是修改函数还是再写个函数都可以的function untree($ar, $pid=0) { $res = array(); foreach((array)$ar as $v) { if(is_numeric(key($v))) { $res = array_merge($res, untree($v)); continue; } if(! isset($v['pid'])) $v['pid'] = $pid; if(isset($v['children'])) { $t = $v['children']; unset($v['children']); } $res[] = $v; if(! empty($t)) $res = array_merge($res, untree($t, $v['id'])); } return $res;}Array( [0] => Array ( [id] => 87073074 [pid] => 0 [text] => 白田最新範疇 ) [1] => Array ( [id] => 67852256 [pid] => 87073074 [text] => 範疇知識 ) [2] => Array ( [id] => 44740741 [pid] => 67852256 [text] => 體能與健康 ) [3] => Array ( [id] => 66256396 [pid] => 44740741 [text] => 小肌肉 ) [4] => Array ( [id] => 71852852 [pid] => 66256396 [text] => 掌握雙手協調能力 ) [5] => Array ( [id] => 84741741 [text] => 能摺錢包 [grade] => 1 [pid] => 71852852 ) [6] => Array ( [id] => 32518528 [pid] => 67852256 [text] => 語文 ) [7] => Array ( [id] => 18185185 [pid] => 32518528 [text] => 聽 ) [8] => Array ( [id] => 35256896 [pid] => 18185185 [text] => 能理解故事內容要點 ) [9] => Array ( [id] => 69295296 [text] => 能理解故事內容要點 [grade] => 1 [pid] => 35256896 ) [10] => Array ( [id] => 54740741 [pid] => 18185185 [text] => 能'聽',指示進行遊戲和活動 ) [11] => Array ( [id] => 93629639 [text] => 能'聽',指示進行活動和遊戲 [grade] => 1 [pid] => 54740741 ) [12] => Array ( [id] => 87073074 [pid] => 0 [text] => 白田最新範疇 ) [13] => Array ( [id] => 67852256 [pid] => 87073074 [text] => 範疇知識 ) [14] => Array ( [id] => 44740741 [pid] => 67852256 [text] => 體能與健康 ) [15] => Array ( [id] => 66256396 [pid] => 44740741 [text] => 小肌肉 ) [16] => Array ( [id] => 71852852 [pid] => 66256396 [text] => 掌握雙手協調能力 ) [17] => Array ( [id] => 84741741 [text] => 能摺錢包 [grade] => 2 [pid] => 71852852 )) 降维后所得$newarr做如下操作,取得相同项的索引,请问我该怎么将对应的索引值相加?unset($keys);foreach($newarr as $key=>$nchild){ if(is_array($nchild) && array_key_exists('grade',$nchild)){ foreach($nchild as $nkey=>$n){ if($nkey=='text'){ $keys[$key]=$n; } } }}unset($result);foreach(array_unique($keys) as $uv){ unset($tmp); foreach($keys as $k=>$v){ if($uv==$v){ $tmp[]=$k; } } $result[$uv]=$tmp;}print_r($result);Array( [能摺錢包] => Array ( [0] => 5 [1] => 23 ) [能理解故事內容要點] => Array ( [0] => 9 [1] => 27 ) [能'聽'指示進行活動和遊戲] => Array ( [0] => 11 ) [能有條理地敍述事物] => Array ( [0] => 14 ) [能掌握雙腳向前跳的技巧] => Array ( [0] => 20 )) 按相同的 text 合并降维结果$ind = array();$res = untree($array);foreach($res as $i=>$v) { $na = $v['text']; if(! isset($ind[$na])) $ind[$na] = $i; else { if(isset($v['grade']) && isset($res[$ind[$na]]['grade'])) { if(! isset($res[$ind[$na]]['count'])) $res[$ind[$na]]['count'] = 1; else $res[$ind[$na]]['count']++; $res[$ind[$na]]['grade'] += $v['grade']; } unset($res[$i]); }}print_r($res);得到Array( [0] => Array ( [id] => 87073074 [pid] => 0 [text] => 白田最新範疇 ) [1] => Array ( [id] => 67852256 [pid] => 87073074 [text] => 範疇知識 ) [2] => Array ( [id] => 44740741 [pid] => 67852256 [text] => 體能與健康 ) [3] => Array ( [id] => 66256396 [pid] => 44740741 [text] => 小肌肉 ) [4] => Array ( [id] => 71852852 [pid] => 66256396 [text] => 掌握雙手協調能力 ) [5] => Array ( [id] => 84741741 [text] => 能摺錢包 [grade] => 3 [pid] => 71852852 [count] => 1 ) [6] => Array ( [id] => 32518528 [pid] => 67852256 [text] => 語文 ) [7] => Array ( [id] => 18185185 [pid] => 32518528 [text] => 聽 ) [8] => Array ( [id] => 35256896 [pid] => 18185185 [text] => 能理解故事內容要點 ) [10] => Array ( [id] => 54740741 [pid] => 18185185 [text] => 能'聽',指示進行遊戲和活動 ) [11] => Array ( [id] => 93629639 [text] => 能'聽',指示進行活動和遊戲 [grade] => 1 [pid] => 54740741 ))按此结果生成树状数组应该就不难了吧? 你给的数据只有一处'0' => array( 'id' => 35256896, 'pid' => 18185185, 'text' => '能理解故事內容要點', 'children' => array( '0' => array( 'id' => 69295296, 'text' => '能理解故事內容要點', 'grade' => 1 )), 是的,但重新组合后应该还有个如“能'聽'指示進行活動和遊戲”一样的子集。如:Array ( [id] => 69295296 [text] => 能理解故事內容要點 [grade] => 1 [pid] => 35256896 [count] => 1 ) 代码没有问题,而是数据有问题 [10] => Array ( [id] => 54740741 [pid] => 18185185 [text] => 能'聽',指示進行遊戲和活動 ) [11] => Array ( [id] => 93629639 [text] => 能'聽',指示進行活動和遊戲 [grade] => 1 [pid] => 54740741 ) 我刚刚发现到贴出来的代码确实跟我实际用到的有出入,sorry。我发私信给你 discuz中未登录收藏的问题,大家进来看下。 imap_open()函数的问题 用户注册信息验证合法性 讨论 RSS 采集的问题 如何得到某一年全年有多少天? 如何吧这些asp转化成php语句 smarty 怎么在模板上循环遍历输出三维数据??? thinkPHP中form表单action的问题 解决select超时问题! mysql5.6无法使用mysqldump自动备份数据求助 写留言版时 <? ............?> 不是用<?php?>么 设置cookie问题
$res = array();
foreach((array)$ar as $v) {
if(is_numeric(key($v))) {
$res = array_merge($res, untree($v, $key, $deep+1, $paren));
continue;
}
$v['deep'] = $deep;
$v['paren'] = $paren;
if(isset($v[$key])) {
$t = $v[$key];
$pa = $v['text'];
unset($v[$key]);
}
$res[] = $v;
if(! empty($t)) $res = array_merge($res, untree($t, $key, $deep+1, $pa));
}
return $res;
}print_r(untree($array));
得
Array
(
[0] => Array
(
[text] => 体育
[deep] => 1
[paren] =>
) [1] => Array
(
[text] => 篮球
[grade] => 1
[deep] => 2
[paren] => 体育
) [2] => Array
(
[text] => 足球
[grade] => 3
[deep] => 2
[paren] => 体育
) [3] => Array
(
[text] => 音乐
[deep] => 1
[paren] =>
) [4] => Array
(
[text] => 唱歌
[deep] => 2
[paren] => 音乐
) [5] => Array
(
[text] => 儿歌三百首
[grade] => 1
[deep] => 3
[paren] => 唱歌
) [6] => Array
(
[text] => 跳舞
[grade] => 3
[deep] => 2
[paren] => 音乐
) [7] => Array
(
[text] => 儿歌三百首
[grade] => 1
[deep] => 3
[paren] => 唱歌
) [8] => Array
(
[text] => 体育
[deep] => 1
[paren] =>
) [9] => Array
(
[text] => 篮球
[grade] => 2
[deep] => 2
[paren] => 体育
) [10] => Array
(
[text] => 排球
[grade] => 5
[deep] => 2
[paren] => 体育
) [11] => Array
(
[text] => 音乐
[deep] => 1
[paren] =>
) [12] => Array
(
[text] => 唱歌
[deep] => 2
[paren] => 音乐
) [13] => Array
(
[text] => 儿歌三百首
[grade] => 4
[deep] => 3
[paren] => 唱歌
))
其实原始数组有记录id ,pid值的
'0' => array(
'0' => array(
'id' => 87073074,
'pid' => 0,
'text' => '白田最新範疇',
'children' => array(
'0' => array(
'id' => 67852256,
'pid' => 87073074,
'text' => '範疇知識',
'children' => array(
'0' => array(
'id' => 44740741,
'pid' => 67852256,
'text' => '體能與健康',
'children' => array(
'0' => array(
'id' => 66256396,
'pid' => 44740741,
'text' => '小肌肉',
'children' => array(
'0' => array(
'id' => 71852852,
'pid' => 66256396,
'text' => '掌握雙手協調能力',
'children' => array(
'0' => array(
'id' => 84741741,
'text' => '能摺錢包',
'grade' => 1
)
)
)
)
)
)
),
'1' => array(
'id' => 32518528,
'pid' => 67852256,
'text' => '語文',
'children' => array(
'0' => array(
'id' => 18185185,
'pid' => 32518528,
'text' => '聽',
'children' => array(
'0' => array(
'id' => 35256896,
'pid' => 18185185,
'text' => '能理解故事內容要點',
'children' => array(
'0' => array(
'id' => 69295296,
'text' => '能理解故事內容要點',
'grade' => 1
)
)
),
'1' => array(
'id' => 54740741,
'pid' => 18185185,
'text' => '能\'聽\',指示進行遊戲和活動',
'children' => array(
'0' => array(
'id' => 93629639,
'text' => '能\'聽\',指示進行活動和遊戲',
'grade' => 1
)
)
)
)
)
)
)
)
)
)
)
),
'1' => array(
'0' => array(
'id' => 87073074,
'pid' => 0,
'text' => '白田最新範疇',
'children' => array(
'0' => array(
'id' => 67852256,
'pid' => 87073074,
'text' => '範疇知識',
'children' => array(
'0' => array(
'id' => 44740741,
'pid' => 67852256,
'text' => '體能與健康',
'children' => array(
'0' => array(
'id' => 66256396,
'pid' => 44740741,
'text' => '小肌肉',
'children' => array(
'0' => array(
'id' => 71852852,
'pid' => 66256396,
'text' => '掌握雙手協調能力',
'children' => array(
'0' => array(
'id' => 84741741,
'text' => '能摺錢包',
'grade' => 2
)
)
)
)
)
)
)
)
)
)
)
)
);
然后是修改函数还是再写个函数都可以的function untree($ar, $pid=0) {
$res = array();
foreach((array)$ar as $v) {
if(is_numeric(key($v))) {
$res = array_merge($res, untree($v));
continue;
}
if(! isset($v['pid'])) $v['pid'] = $pid;
if(isset($v['children'])) {
$t = $v['children'];
unset($v['children']);
}
$res[] = $v;
if(! empty($t)) $res = array_merge($res, untree($t, $v['id']));
}
return $res;
}Array
(
[0] => Array
(
[id] => 87073074
[pid] => 0
[text] => 白田最新範疇
) [1] => Array
(
[id] => 67852256
[pid] => 87073074
[text] => 範疇知識
) [2] => Array
(
[id] => 44740741
[pid] => 67852256
[text] => 體能與健康
) [3] => Array
(
[id] => 66256396
[pid] => 44740741
[text] => 小肌肉
) [4] => Array
(
[id] => 71852852
[pid] => 66256396
[text] => 掌握雙手協調能力
) [5] => Array
(
[id] => 84741741
[text] => 能摺錢包
[grade] => 1
[pid] => 71852852
) [6] => Array
(
[id] => 32518528
[pid] => 67852256
[text] => 語文
) [7] => Array
(
[id] => 18185185
[pid] => 32518528
[text] => 聽
) [8] => Array
(
[id] => 35256896
[pid] => 18185185
[text] => 能理解故事內容要點
) [9] => Array
(
[id] => 69295296
[text] => 能理解故事內容要點
[grade] => 1
[pid] => 35256896
) [10] => Array
(
[id] => 54740741
[pid] => 18185185
[text] => 能'聽',指示進行遊戲和活動
) [11] => Array
(
[id] => 93629639
[text] => 能'聽',指示進行活動和遊戲
[grade] => 1
[pid] => 54740741
) [12] => Array
(
[id] => 87073074
[pid] => 0
[text] => 白田最新範疇
) [13] => Array
(
[id] => 67852256
[pid] => 87073074
[text] => 範疇知識
) [14] => Array
(
[id] => 44740741
[pid] => 67852256
[text] => 體能與健康
) [15] => Array
(
[id] => 66256396
[pid] => 44740741
[text] => 小肌肉
) [16] => Array
(
[id] => 71852852
[pid] => 66256396
[text] => 掌握雙手協調能力
) [17] => Array
(
[id] => 84741741
[text] => 能摺錢包
[grade] => 2
[pid] => 71852852
))
foreach($newarr as $key=>$nchild){
if(is_array($nchild) && array_key_exists('grade',$nchild)){
foreach($nchild as $nkey=>$n){
if($nkey=='text'){
$keys[$key]=$n;
}
}
}
}unset($result);
foreach(array_unique($keys) as $uv){
unset($tmp);
foreach($keys as $k=>$v){
if($uv==$v){
$tmp[]=$k;
}
}
$result[$uv]=$tmp;
}
print_r($result);
Array
(
[能摺錢包] => Array
(
[0] => 5
[1] => 23
) [能理解故事內容要點] => Array
(
[0] => 9
[1] => 27
) [能'聽'指示進行活動和遊戲] => Array
(
[0] => 11
) [能有條理地敍述事物] => Array
(
[0] => 14
) [能掌握雙腳向前跳的技巧] => Array
(
[0] => 20
))
$res = untree($array);
foreach($res as $i=>$v) {
$na = $v['text'];
if(! isset($ind[$na])) $ind[$na] = $i;
else {
if(isset($v['grade']) && isset($res[$ind[$na]]['grade'])) {
if(! isset($res[$ind[$na]]['count'])) $res[$ind[$na]]['count'] = 1;
else $res[$ind[$na]]['count']++;
$res[$ind[$na]]['grade'] += $v['grade'];
}
unset($res[$i]);
}
}
print_r($res);
得到
Array
(
[0] => Array
(
[id] => 87073074
[pid] => 0
[text] => 白田最新範疇
) [1] => Array
(
[id] => 67852256
[pid] => 87073074
[text] => 範疇知識
) [2] => Array
(
[id] => 44740741
[pid] => 67852256
[text] => 體能與健康
) [3] => Array
(
[id] => 66256396
[pid] => 44740741
[text] => 小肌肉
) [4] => Array
(
[id] => 71852852
[pid] => 66256396
[text] => 掌握雙手協調能力
) [5] => Array
(
[id] => 84741741
[text] => 能摺錢包
[grade] => 3
[pid] => 71852852
[count] => 1
) [6] => Array
(
[id] => 32518528
[pid] => 67852256
[text] => 語文
) [7] => Array
(
[id] => 18185185
[pid] => 32518528
[text] => 聽
) [8] => Array
(
[id] => 35256896
[pid] => 18185185
[text] => 能理解故事內容要點
) [10] => Array
(
[id] => 54740741
[pid] => 18185185
[text] => 能'聽',指示進行遊戲和活動
) [11] => Array
(
[id] => 93629639
[text] => 能'聽',指示進行活動和遊戲
[grade] => 1
[pid] => 54740741
))按此结果生成树状数组应该就不难了吧?
'0' => array(
'id' => 35256896,
'pid' => 18185185,
'text' => '能理解故事內容要點',
'children' => array(
'0' => array(
'id' => 69295296,
'text' => '能理解故事內容要點',
'grade' => 1
)
),
Array
(
[id] => 69295296
[text] => 能理解故事內容要點
[grade] => 1
[pid] => 35256896
[count] => 1
)
[10] => Array
(
[id] => 54740741
[pid] => 18185185
[text] => 能'聽',指示進行遊戲和活動
) [11] => Array
(
[id] => 93629639
[text] => 能'聽',指示進行活動和遊戲
[grade] => 1
[pid] => 54740741
)
我刚刚发现到贴出来的代码确实跟我实际用到的有出入,sorry。我发私信给你