Header (' content-type:text/html; Charset=utf-8 ');//The following is the original array $array = Array (0=>array (' text ' = ' Sports ', ' Children ' + = Array (' 0=>array ' (' text ' = ' = ' basketball ', ' grade ' = ' 1 '), 1=>array (' text ' = = ' soccer ', ' grade ' = ' 3 ')) ), 1=>array (' text ' = ' music ', ' children ' = = Array (0=>array (' text ' = = ' singing ', ' children ' = ' = ' Array (0=> Array (' text ' = ' 300 song ', ' Grade ' = ' 1 ')), 1=>array (' text ' = ' dance ', ' grade ' = ' 3 '))), 1=>array (0=> Array (' text ' = = ' sports ', ' children ' = = Array (' 0=>array ' = ' text ' = ' basketball ', ' grade ' = ' 2 '), 1=>array (' text ' = > ' volleyball ', ' Grade ' + ' 5 ')), 1=>array (' text ' = ' music ', ' children ' = ' = ' Array (' text ' = ' = ' singing ', ' Children ' = = Array (' 0=>array (' text ' = ' 300 ', ' grade ' = ' 4 '))))));//requirements: recursively traverse the original multidimensional array, merge duplicate key values and accumulate grade values, and record the number of repetitions. Such as: Sports, basketball, after processing grade value is 1+2=3, the number of repetitions is 2 (note: Multidimensional array level variable)//the following is the desired result $newarray = Array (0=>array (' text ' = > ' Sports ', ' children ' = = Array (0=>array (' text ' = ' basketball ', ' graDe ' + ' 3 ', ' count ' = ' 2 '), 1=>array (' text ' = ' = ' Football ', ' grade ' = ' 3 ', ' count ' = ' 1 '), 2=>array (' text ' =& Gt ' Volleyball ', ' grade ' = ' 5 ', ' count ' = ' 1 ')), 1=>array (' text ' = ' music ', ' children ' = ' = ' = ' = ' = ' > ' singing ', ' children ' = = Array (' 0=>array ' (' text ' = ' 300 ', ' grade ' = ' 5 ', ' count ' = ' 2 ')), 1=>array (' Text ' = ' dance ', ' grade ' = ' 3 ', ' count ' = ' 1 ')));
Reply to discussion (solution)
Seems to have sex, and so the master solved, study and study
There's no need for recursion. The double loop is perfectly fine.
That's the basis.
There's no need for recursion. The double loop is perfectly fine.
But the hierarchy of arrays is not necessarily, how much is actually 5 to 6 levels
There's no need for recursion. The double loop is perfectly fine.
But the hierarchy of arrays is not necessarily, how much is actually 5 to 6 levels
But the hierarchy of arrays is not necessarily, there are actually as many as 5 to 6 levels
Where does your data come from?
It's easy to do it, it's a little harder to do it like you do.
Don't worry, I'll see you when you're free.
Last night to study the topic 6 hours, finally holding a reference manual to find various array functions, the result is not done.
Where does your data come from?
It's easy to do it, it's a little harder to do it like you do.
Don't worry, I'll see you when you're free.
Thank you Moderator
Last night to study the topic 6 hours, finally holding a reference manual to find various array functions, the result is not done.
No, it took me 2 days to find a way out.
Give a thought: the first dimensionality reduction
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));
Have
Array
(
[0] = = Array
(
[Text] = Sports
[Deep] = 1
[Paren] =
)
[1] = = Array
(
[Text] = Basketball
[Grade] = 1
[Deep] = 2
[Paren] = Sports
)
[2] = = Array
(
[Text] = Football
[Grade] = 3
[Deep] = 2
[Paren] = Sports
)
[3] = = Array
(
[Text] = music
[Deep] = 1
[Paren] =
)
[4] = = Array
(
[Text] = Sing
[Deep] = 2
[Paren] = music
)
[5] = = Array
(
[Text] = 300 songs
[Grade] = 1
[Deep] = 3
[Paren] = Sing
)
[6] = = Array
(
[Text] = dancing
[Grade] = 3
[Deep] = 2
[Paren] = music
)
[7] = = Array
(
[Text] = 300 songs
[Grade] = 1
[Deep] = 3
[Paren] = Sing
)
[8] = = Array
(
[Text] = Sports
[Deep] = 1
[Paren] =
)
[9] = = Array
(
[Text] = Basketball
[Grade] = 2
[Deep] = 2
[Paren] = Sports
)
[Ten] + = Array
(
[Text] = Volleyball
[Grade] = 5
[Deep] = 2
[Paren] = Sports
)
[one] = = Array
(
[Text] = music
[Deep] = 1
[Paren] =
)
[+] = Array
(
[Text] = Sing
[Deep] = 2
[Paren] = music
)
[+] = Array
(
[Text] = 300 songs
[Grade] = 4
[Deep] = 3
[Paren] = Sing
)
)
Where does your data come from?
It's easy to do it, it's a little harder to do it like you do.
Don't worry, I'll see you when you're free.
The data is returned through another wrapper function (I cannot modify the encapsulation function)
Give a thought: the first dimensionality reduction
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));
Have
Array
(
[0] = = Array
(
[Text] = Sports
[Deep] = 1
[Paren] =
)
[1] = = Array
(
[Text] = Basketball
[Grade] = 1
[Deep] = 2
[Paren] = Sports
)
[2] = = Array
(
[Text] = Football
[Grade] = 3
[Deep] = 2
[Paren] = Sports
)
[3] = = Array
(
[Text] = music
[Deep] = 1
[Paren] =
)
[4] = = Array
(
[Text] = Sing
[Deep] = 2
[Paren] = music
)
[5] = = Array
(
[Text] = 300 songs
[Grade] = 1
[Deep] = 3
[Paren] = Sing
)
[6] = = Array
(
[Text] = dancing
[Grade] = 3
[Deep] = 2
[Paren] = music
)
[7] = = Array
(
[Text] = 300 songs
[Grade] = 1
[Deep] = 3
[Paren] = Sing
)
[8] = = Array
(
[Text] = Sports
[Deep] = 1
[Paren] =
)
[9] = = Array
(
[Text] = Basketball
[Grade] = 2
[Deep] = 2
[Paren] = Sports
)
[Ten] + = Array
(
[Text] = Volleyball
[Grade] = 5
[Deep] = 2
[Paren] = Sports
)
[one] = = Array
(
[Text] = music
[Deep] = 1
[Paren] =
)
[+] = Array
(
[Text] = Sing
[Deep] = 2
[Paren] = music
)
[+] = Array
(
[Text] = 300 songs
[Grade] = 4
[Deep] = 3
[Paren] = Sing
)
)
In fact, the original array has record ID, PID value
Then you've got the data completely intact.
$array =array (' 0 ' = = Array (' 0 ' = = Array (' id ' = 87073074, ' PID ' = 0, ' text ' = ' Pak tin newest?? ', ' children ' = = Array ( ' 0 ' = = Array (' id ' = = 67852256, ' pid ' => ; 87073074, ' text ' = '?? Know? ', ' children ' = = Array (' 0 ' = = array (' id ' = 44740741, ' pid ' = = 67852256, ' text ' = '? Health ', ' Children ' = = Array (' 0 ' = AR Ray (' id ' = =66256396, ' pid ' = 44740741, ' Text ' = ' small muscles ', ' Children ' = = Array (' 0 ' = Arra Y (' id ' = = 71852852, ' pid ' = 66256396, ' text ' and ' = ' master?? Ability ', ' children ' = = Array ( ' 0 ' = = Array (' id ' = = 84741741, ' t Ext ' = ' can pack ', ' gra De ' = 1) ) ) ) ) ) ), ' 1 ' = = Array ( ' id ' = = 32518528, ' pid ' = 67852256, ' Text ' = '? ', ' children ' = = Array ( ' 0 ' = = Array ( ' id ' = = 18185185, ' pid ' = 32518 528, ' text ' = '? ', ' Children ' = Array ( ' 0 ' = = Array (' id ' = = 35256896, ' pid ' = 18185185, ' Text ' = ' Can you understand the story? ' ' Children ' = Array ( ' 0 ' = = Array ( ' id ' = 69295296, ' Text ' = ' Can you understand the story? ' ' Grade ' = 1 ) ) ), ' 1 ' = = Array (' ID ' = 54740741, ' pid ' = 18185185, ' text ' and ' = ' can \ '? ', indicate?? And live? ', ' children ' = = Array ( ' 0 ' = = Array ( ' id ' = 93629639, ' text ' and ' = ' can \ '? ', indicating the line of work and?? ', ' Grade ' = 1 ) ) ) ) ) ) ) ) ))), ' 1 ' = = Array (' 0 ' = = Array ( ' id ' = = 87073074, ' pid ' = 0, ' text ' = ' Pak tin latest?? ', ' Children ' = = Array (' 0 ' = = Array (' ID ' => ; 67852256, ' pid ' = = 87073074, ' text ' = '?? Know? ', ' children ' = = Array (' 0 ' = = array ( ' id ' = = 44740741, ' pid ' = 67852256, ' Text ' = '? ' Children ' = = Array (' 0 ' = = Array ( ' id ' = 66256396, ' pid ' = 44740741, ' text ' = ' small muscles ', ' Children ' = Array ( ' 0 ' = = Array ( ' id ' = 71852852, ' pid ' = 66256396, ' text ' and ' = ' master?? Ability ', ' children ' = = Array ( ' 0 ' = = Array (