Php recursively traverses multi-dimensional arrays, merges repeated values, and records repeated times

Source: Internet
Author: User
Php recursively traverses multi-dimensional arrays, merges repeated values, and records repeated times
Header ('content-type: text/html; charset = utf-8 '); // The following is the original array $ array = array (0 => array (0 => array ('text' => 'sport ', 'Children '=> array (0 => array ('text' => 'Basketball', 'grade '=> '1 '), 1 => array ('text' => 'soccer ', 'grade' => '3'), 1 => array ('text' => 'Music ', 'Children '=> array (0 => array ('text' => 'change ', 'Children '=> array (0 => array ('text' => 'Children's song three hundred topic', 'grade '=> '1 '))), 1 => array ('text' => 'Dancing ', 'grade' => '3'), 1 => array (0 => array ('Text' => 'Sports ', 'Children' => array (0 => array ('text' => 'Basketball ', 'Grad' => '2'), 1 => array ('text' => 'volleyball ', 'Grad' => '5 '))), 1 => array ('text' => 'Music', 'Children '=> array (0 => array ('text' => 'change ', 'Children '=> array (0 => array ('text' => 'Children's song three hundred topic', 'grade '=> '4 ')))))),); // requirement: Recursively traverse the original multi-dimensional array, combine duplicate key values, accumulate grade values, and repeat times. For example, for sports> basketball, after processing, the grade value is 1 + 2 = 3, and the number of duplicates is 2 (note: The multi-dimensional array hierarchy is not fixed) // The following is the expected result $ newarray = array (0 => array (0 => array ('text' => 'sport ', 'Children '=> array (0 => array ('text' => 'Basketball', 'grade '=> '3', 'count' => '2 '), 1 => array ('text' => 'soccer ', 'grade' => '3', 'count' => '1 '), 2 => array ('text' => 'volleyball ', 'grade' => '5', 'count' => '1 '))), 1 => array ('text' => 'Music', 'Children '=> array (0 => array ('text' => 'change ', 'Children '=> array (0 => array ('text' => 'Children's song three hundred topic', 'grade '=> '5 ', 'Count' => '2'), 1 => array ('text' => 'Dancing ', 'grade' => '3 ', 'Count' => '1 ')))));


Reply to discussion (solution)

It seems like you have sex. you can learn from it after you have done it.

Recursion is not required. The dual loop can be done completely.

This is the foundation.

Recursion is not required. The dual loop can be done completely.


But the hierarchy of the array is not necessarily, actually there are 5 to 6 levels


Recursion is not required. The dual loop can be done completely.


But the hierarchy of the array is not necessarily, actually there are 5 to 6 levels


However, the arrays are not necessarily hierarchical. actually there are 5 to 6 levels.

Where did your data come from?
It's easy to do. it's difficult to do it like you do.
Don't worry. you can check it out later.

I studied this question for six hours last night and finally found various array functions with reference manuals.

Where did your data come from?
It's easy to do. it's difficult to do it like you do.
Don't worry. you can check it out later.



Thanks to the moderator

I studied this question for six hours last night and finally found various array functions with reference manuals.



No. it took me two days to find a way out.

Give an idea: First reduce dimensionality

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 ));
Get
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] => singing
[Deep] => 2
[Paren] => Music
)

[5] => Array
(
[Text] => three hundred children's songs
[Grade] => 1
[Deep] => 3
[Paren] => singing
)

[6] => Array
(
[Text] => dancing
[Grade] => 3
[Deep] => 2
[Paren] => Music
)

[7] => Array
(
[Text] => three hundred children's songs
[Grade] => 1
[Deep] => 3
[Paren] => singing
)

[8] => Array
(
[Text] => Sports
[Deep] => 1
[Paren] =>
)

[9] => Array
(
[Text] => Basketball
[Grade] => 2
[Deep] => 2
[Paren] => Sports
)

[10] => Array
(
[Text] => Volleyball
[Grade] => 5
[Deep] => 2
[Paren] => Sports
)

[11] => Array
(
[Text] => Music
[Deep] => 1
[Paren] =>
)

[12] => Array
(
[Text] => singing
[Deep] => 2
[Paren] => Music
)

[13] => Array
(
[Text] => three hundred children's songs
[Grade] => 4
[Deep] => 3
[Paren] => singing
)

)

Where did your data come from?
It's easy to do. it's difficult to do it like you do.
Don't worry. you can check it out later.



Data is returned through another encapsulation function (I cannot modify the encapsulation function)

Give an idea: First reduce dimensionality

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 ));
Get
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] => singing
[Deep] => 2
[Paren] => Music
)

[5] => Array
(
[Text] => three hundred children's songs
[Grade] => 1
[Deep] => 3
[Paren] => singing
)

[6] => Array
(
[Text] => dancing
[Grade] => 3
[Deep] => 2
[Paren] => Music
)

[7] => Array
(
[Text] => three hundred children's songs
[Grade] => 1
[Deep] => 3
[Paren] => singing
)

[8] => Array
(
[Text] => Sports
[Deep] => 1
[Paren] =>
)

[9] => Array
(
[Text] => Basketball
[Grade] => 2
[Deep] => 2
[Paren] => Sports
)

[10] => Array
(
[Text] => Volleyball
[Grade] => 5
[Deep] => 2
[Paren] => Sports
)

[11] => Array
(
[Text] => Music
[Deep] => 1
[Paren] =>
)

[12] => Array
(
[Text] => singing
[Deep] => 2
[Paren] => Music
)

[13] => Array
(
[Text] => three hundred children's songs
[Grade] => 4
[Deep] => 3
[Paren] => singing
)

)



In fact, the original array has a record id and a pid value.

Then you can complete the data.

$ Array = array ('0' => array ('0' => array ('id' => 87073074, 'pid '=> 0, 'text' => 'white Tian latest ?? ', 'Children' => array ('0' => array ('id' => 67852256, 'pid '=> 87073074, 'text' => '?? Zhi? ', 'Children' => array ('0' => array ('id' => 44740741, 'pid '=> 67852256, 'text' => '? Yes? Healthy ', 'Children' => array ('0' => array ('id' => 66256396, 'pid '=> 44740741, 'text' => 'Little mysql', 'Children '=> array ('0' => array ('id' => 71852852, 'pid' => 66256396, 'text' => 'master? Hand ?? Capabilities ', 'Children' => array ('0' => array ('id' => 84741741, 'text' => 'can be merged? Package ', 'grade' => 1), '1' => array ('id' => 32518528, 'pid '=> 67852256, 'text' => '? Wen ', 'Children' => array ('0' => array ('id' => 18185185, 'pid '=> 32518528, 'text' => '? ', 'Children' => array ('0' => array ('id' => 35256896, 'pid '=> 18185185, 'text' =>' can you understand the story? Can you? ', 'Children' => array ('0' => array ('id' => 69295296, 'text' => 'can you understand the story? Can you? ', 'Grad' => 1), '1' => array ('id' => 54740741, 'pid' => 18185185, 'text' => 'Can \'? \ ', Indication? OK ?? And live? ', 'Children' => array ('0' => array ('id' => 93629639, 'text' => 'Can \'? \ ', Indication? Live? And ?? ', 'Grad' => 1 )))))))))))), '1' => array ('0' => array ('id' => 87073074, 'pid '=> 0, 'text' => 'white Tian latest ?? ', 'Children' => array ('0' => array ('id' => 67852256, 'pid '=> 87073074, 'text' => '?? Zhi? ', 'Children' => array ('0' => array ('id' => 44740741, 'pid '=> 67852256, 'text' => '? Yes? Healthy ', 'Children' => array ('0' => array ('id' => 66256396, 'pid '=> 44740741, 'text' => 'Little mysql', 'Children '=> array ('0' => array ('id' => 71852852, 'pid' => 66256396, 'text' => 'master? Hand ?? Capabilities ', 'Children' => array ('0' => array (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.