搜索
首页后端开发php教程php递归遍历多维数组,合并重复值并记录重复次数

header('Content-type: text/html; charset=utf-8');//以下是原始数组$array = array(	0=>array(		0=>array(			'text' => '体育',			'children' => array(				0=>array(					'text' => '篮球',					'grade' => '1'				),				1=>array(					'text' => '足球',					'grade' => '3'				)			)		),		1=>array(			'text' => '音乐',			'children' => array(				0=>array(					'text' => '唱歌',					'children' => array(						0=>array(							'text' => '儿歌三百首',							'grade' => '1'						)					)				),				1=>array(					'text' => '跳舞',					'grade' => '3'				)			)		)	),	1=>array(		0=>array(			'text' => '体育',			'children' => array(				0=>array(					'text' => '篮球',					'grade' => '2'				),				1=>array(					'text' => '排球',					'grade' => '5'				)			)		),		1=>array(			'text' => '音乐',			'children' => array(				0=>array(					'text' => '唱歌',					'children' => array(						0=>array(							'text' => '儿歌三百首',							'grade' => '4'						)					)				)			)		)	),);//要求:递归遍历原始多维数组,将重复键值合并并累加grade值,以及记录重复次数。如:体育->篮球,经过处理后grade值为1+2=3,重复个数则为2(注:多维数组层级不定)//以下是想要的到的结果$newarray = array(	0=>array(		0=>array(			'text' => '体育',			'children' => array(				0=>array(					'text' => '篮球',					'grade' => '3',					'count' => '2'				),				1=>array(					'text' => '足球',					'grade' => '3',					'count' => '1'				),				2=>array(					'text' => '排球',					'grade' => '5',					'count' => '1'				)			)		),		1=>array(			'text' => '音乐',			'children' => array(				0=>array(					'text' => '唱歌',					'children' => array(						0=>array(							'text' => '儿歌三百首',							'grade' => '5',							'count' => '2'						)					)				),				1=>array(					'text' => '跳舞',					'grade' => '3',					'count' => '1'				)			)		)	));


回复讨论(解决方案)

貌似有性,等高手解决了,学习学习

不需要递归吧。双重循环完全可以搞定。

这就是基础了

不需要递归吧。双重循环完全可以搞定。


但数组的层级是不一定的,实际上有多大5至6级


不需要递归吧。双重循环完全可以搞定。


但数组的层级是不一定的,实际上有多大5至6级
但数组的层级是不一定的,实际上有多达5至6级

你的数据是从哪里来的?
正着做很简单,像你这样反着做是有些难度的
别着急,有空帮你看看

昨天晚上研究这个题目6个小时,最后抱着参考手册找各种数组函数,结果还是没做出来。

你的数据是从哪里来的?
正着做很简单,像你这样反着做是有些难度的
别着急,有空帮你看看



多谢版主

昨天晚上研究这个题目6个小时,最后抱着参考手册找各种数组函数,结果还是没做出来。



不才,我花了2天时间也还是没能找着出路

给个思路:先降维

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] => 唱歌
        )

)

你的数据是从哪里来的?
正着做很简单,像你这样反着做是有些难度的
别着急,有空帮你看看



数据是通过另一个封装函数返回的(我不能修改封装函数)

给个思路:先降维

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(                        
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
2 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。