Php calculates the total number of equal values in a two-dimensional array and combines them into a new array.

Source: Internet
Author: User
Php calculates the total number of equal values in a two-dimensional array and combines them into a new php multi-dimensional array.


For example, I want to combine the m_id equivalent array elements in this array into a new array:



Reply to discussion (solution)

For example, you need to justify yourself. otherwise there will be misunderstandings.

The raw data does not include:
'Sv _ id' => 5, 'mid '=> 0
'Sv _ id' => 9, 'mid '=> 3
And how are the results displayed?

For example, you need to justify yourself. otherwise there will be misunderstandings.

The raw data does not include:
'Sv _ id' => 5, 'mid '=> 0
'Sv _ id' => 9, 'mid '=> 3
And how are the results displayed?

Yes, but the data can be changed at will. Take a look at the array structure of the two graphs...

This structure does not matter, right?
From an algorithm perspective: How to generate 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 can copy and run it to see if it is the effect you want.
:



Don't consider your extra projects (I can't guess)

$ar = array(  array( 'sv_id' => 1, 'mid' => 1, 'sv_question1' => 20 ),  array( 'sv_id' => 2, 'mid' => 2, 'sv_question1' => 10 ),  array( 'sv_id' => 3, 'mid' => 1, 'sv_question1' => 20 ),  array( 'sv_id' => 4, 'mid' => 3, 'sv_question1' => 20 ),  array( 'sv_id' => 5, 'mid' => 2, 'sv_question1' => 20 ),);$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] => 20                )            [3] => Array                (                    [sv_id] => 3                    [mid] => 1                    [sv_question1] => 20                )        )    [2] => Array        (            [2] => Array                (                    [sv_id] => 2                    [mid] => 2                    [sv_question1] => 10                )            [5] => Array                (                    [sv_id] => 5                    [mid] => 2                    [sv_question1] => 20                )        )    [3] => Array        (            [4] => Array                (                    [sv_id] => 4                    [mid] => 3                    [sv_question1] => 20                )        ))

Wow, #5 Moderator, why can't I think of your writing method .. Your simplicity.

  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 can copy and run it to see if it is the effect you want.
:



Well, this method is acceptable. thank you, but the following great gods are even more terrible. it's too simple !!!

Don't consider your extra projects (I can't guess)

$ar = array(  array( 'sv_id' => 1, 'mid' => 1, 'sv_question1' => 20 ),  array( 'sv_id' => 2, 'mid' => 2, 'sv_question1' => 10 ),  array( 'sv_id' => 3, 'mid' => 1, 'sv_question1' => 20 ),  array( 'sv_id' => 4, 'mid' => 3, 'sv_question1' => 20 ),  array( 'sv_id' => 5, 'mid' => 2, 'sv_question1' => 20 ),);$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] => 20                )            [3] => Array                (                    [sv_id] => 3                    [mid] => 1                    [sv_question1] => 20                )        )    [2] => Array        (            [2] => Array                (                    [sv_id] => 2                    [mid] => 2                    [sv_question1] => 10                )            [5] => Array                (                    [sv_id] => 5                    [mid] => 2                    [sv_question1] => 20                )        )    [3] => Array        (            [4] => Array                (                    [sv_id] => 4                    [mid] => 3                    [sv_question1] => 20                )        ))



Awesome !!! Thank you !!

This structure does not matter, right?
From an algorithm perspective: How to generate items that are not in the original data

Yes, I wrote an error in sorry. I wrote mid with an underscore m_id missing.


   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 can copy and run it to see if it is the effect you want.
:



Well, this method is acceptable. thank you, but the following great gods are even more terrible. it's 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 can copy and run it to see if it is the effect you want.
:



Well, this method is acceptable. thank you, but the following great gods are even more terrible. it's too simple !!!

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.