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

Source: Internet
Author: User
Php calculates the total number of equal values in a two-dimensional array and combines them into a new array. for example, I want to & nbsp; m_id & nbsp; the equal array elements are combined into a new array: php? Multi-dimensional array? Php calculates the total number of equal values in a two-dimensional array and combines them into a new array.

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


Php? Multi-dimensional array?

Share:


------ Solution --------------------
 $ Arr = array (
Array (
'Sv _ id' => 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)
------ Solution --------------------
! 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.
:




------ Solution --------------------
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;

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.