PHP counts the total number of equal values in a two-dimensional array and combines them into a new array

Source: Internet
Author: User
PHP Multidimensional arrays


For example, I would like to combine the elements of this array into a new array of m_id equal arrays: The following:



Reply to discussion (solution)

For example, you have to justify it, or it will cause misunderstanding.

The original data does not:
' sv_id ' = 5, ' mid ' + 0
' sv_id ' = 9, ' Mid ' + 3
, how did the results come out?

For example, you have to justify it, or it will cause misunderstanding.

The original data does not:
' sv_id ' = 5, ' mid ' + 0
' sv_id ' = 9, ' Mid ' + 3
, how did the results come out?

Well, yes, but the data can be changed casually ah, take a good look at the two graphs of the array structure it ...

This structure doesn't matter, does it?
From an algorithmic point of view: How to produce items that are not in the original data

 1, ' m_id ' =>1, ' Sv_question1 ' =>20), array (' sv_id ' =>2, ' m_id ' =>2, ' Sv_question1 ' =>10), array (' sv_id ' = >3, ' m_id ' =>1, ' Sv_question1 ' =>20), array (' sv_id ' =>4, ' m_id ' =>3, ' Sv_question1 ' =>20), array (' Sv_ Id ' =>5, ' m_id ' =>2, ' Sv_question1 ' =>10);//var_dump ($arr); $ar =array (); foreach ($arr as $v) {if (empty ($ar) | | | In_array ($v, $ar)) {$ar [$v [' m_id ']][$v [' sv_id ']]= $v;} Else{foreach ($ar as $ka = $va) {if ($ka = = $v [' m_id ']) {$ar [$ka] [$v [' sv_id ']]= $v;}}} echo "
"; Var_dump ($ar);

You copy run to see if you want the effect.




Don't consider your extra items (I can't figment)

 $ar = Array (' sv_id ' = = 1, ' mid ' = + 1, ' sv_question1 ' = =), Array (' s v_id ' + 2, ' mid ' and ' 2 ', ' sv_question1 ', ' + ', Array (' sv_id ' = 3, ' mid ' = 1, ' sv_question1 ' + 20) , Array (' sv_id ' = 4, ' mid ' = + 3, ' sv_question1 ' = ' = '), array (' sv_id ' = 5, ' mid ' = + 2, ' Sv_question1 ' = = '), $res = Array (), foreach ($ar as $item) {$res [$item [' Mid ']][$item [' sv_id '] = $item;} Print_r ($res); 
Array ([1] = = Array ([1] = = Array ([sv_id] = 1                [Mid] = 1 [Sv_question1] = [3] = = Array                ([sv_id] = 3 [Mid] = 1 [sv_question1] 20 )) [2] = = Array ([2] = = Array ([sv_id] = > 2 [mid] = 2 [Sv_question1] [5] = A Rray ([sv_id] = 5 [Mid] = 2 [sv_question1                    ] = [3] = = Array ([4] = = Array (        [SV_ID] = 4 [Mid] = 3 [Sv_question1] + 20) ))

Wow, #5版主, why can't I think of your way of writing? Your good is concise.

  1, ' m_id ' =>1, ' Sv_question1 ' =>20), array (' sv_id ' =>2, ' m_id ' =>2, ' Sv_question1 ' =>10), array (' sv_id ' = >3, ' m_id ' =>1, ' Sv_question1 ' =>20), array (' sv_id ' =>4, ' m_id ' =>3, ' Sv_question1 ' =>20), array (' Sv_ Id ' =>5, ' m_id ' =>2, ' Sv_question1 ' =>10);//var_dump ($arr); $ar =array (); foreach ($arr as $v) {if (empty ($ar) | | | In_array ($v, $ar)) {$ar [$v [' m_id ']][$v [' sv_id ']]= $v;} Else{foreach ($ar as $ka = $va) {if ($ka = = $v [' m_id ']) {$ar [$ka] [$v [' sv_id ']]= $v;}}} echo "
"; Var_dump ($ar);

You copy run to see if you want the effect.




Well, this way can, thanks, but the following big God is more scary, too simple!!!

Don't consider your extra items (I can't figment)

$ar = Array ('  sv_id ' = = 1, ' mid ' = = 1, ' sv_question1 ' = ' = '),  array (' sv_id ' = 2, ' mid ' = +) 2, ' sv_question1 ' = +,  array (' sv_id ' = + 3, ' mid ' = 1, ' sv_question1 ', ' = '),  array (' sv_id ' =& Gt  4, ' mid ' = + 3, ' sv_question1 ', ' = ', '  array ' (' sv_id ' = 5, ' mid ' = 2, ' sv_question1 ', ' = ') '; $res = Array (); foreach ($ar as $item) {  $res [$item [' Mid ']][$item [' sv_id '] = $item;} Print_r ($res);
Array ([1] = = Array ([1] = = Array ([sv_id] = 1                [Mid] = 1 [Sv_question1] = [3] = = Array                ([sv_id] = 3 [Mid] = 1 [sv_question1] 20 )) [2] = = Array ([2] = = Array ([sv_id] = > 2 [mid] = 2 [Sv_question1] [5] = A Rray ([sv_id] = 5 [Mid] = 2 [sv_question1                    ] = [3] = = Array ([4] = = Array (        [SV_ID] = 4 [Mid] = 3 [Sv_question1] + 20) ))



Too cow!!! Worship Thank you!!

This structure doesn't matter, does it?
From an algorithmic point of view: How to produce items that are not in the original data

Well, yes, sorry I wrote it wrong, one less underline m_id written in mid


   1, ' m_id ' =>1, ' Sv_question1 ' =>20), array (' sv_id ' =>2, ' m_id ' =>2, ' Sv_question1 ' =>10), array (' sv_id ' = >3, ' m_id ' =>1, ' Sv_question1 ' =>20), array (' sv_id ' =>4, ' m_id ' =>3, ' Sv_question1 ' =>20), array (' Sv_ Id ' =>5, ' m_id ' =>2, ' Sv_question1 ' =>10);//var_dump ($arr); $ar =array (); foreach ($arr as $v) {if (empty ($ar) | | | In_array ($v, $ar)) {$ar [$v [' m_id ']][$v [' sv_id ']]= $v;} Else{foreach ($ar as $ka = $va) {if ($ka = = $v [' m_id ']) {$ar [$ka] [$v [' sv_id ']]= $v;}}} echo "
"; Var_dump ($ar);

You copy run to see if you want the effect.




Well, this way can, thanks, but the following big God is more scary, too simple!!!



 
     1, ' m_id ' =>1, ' Sv_question1 ' =>20), array (' sv_id ' =>2, ' m_id ' =>2, ' Sv_question1 ' =>10), array (' sv_id ' = >3, ' m_id ' =>1, ' Sv_question1 ' =>20), array (' sv_id ' =>4, ' m_id ' =>3, ' Sv_question1 ' =>20), array (' Sv_ Id ' =>5, ' m_id ' =>2, ' Sv_question1 ' =>10);//var_dump ($arr); $ar =array (); foreach ($arr as $v) {if (empty ($ar) | | | In_array ($v, $ar)) {$ar [$v [' m_id ']][$v [' sv_id ']]= $v;} Else{foreach ($ar as $ka = $va) {if ($ka = = $v [' m_id ']) {$ar [$ka] [$v [' sv_id ']]= $v;}}} echo "
"; Var_dump ($ar);

You copy run to see if you want the effect.




Well, this way can, thanks, but the following big God is more scary, too simple!!!
  • Related Article

    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.