Arrays are composed of multiple arrays based on the same Val value

Source: Internet
Author: User
The original array is similar
$arr        =   Array (            ' id ' = ' 1 '  ,                ' name ' = '  a ',                ' project '   =  ' 1 ',            ),            array (                ' id '    = '  2 ',                ' name '      = '  B ',                ' Project '   =  ' 3 ',            ),            array (                ' id '    = '  3 ',                ' name '      = ' =  ') C ',                ' project '   =  ' 2 ',            ),            array (                ' id '  = ' 4 ',                ' name '      = >  ' d ',                ' project '   =  ' 1 ',            ),        );

I want to make multiple arrays based on the same nature of project, such as the following results
$arr 1        =   Array (            ' id ' = '  1 ',                ' name ' = '  a ',                ' project '   =  ' 1 ',            ),            array (                ' id '    = '  4 ',                ' name '      = '  d ',                ' project '   =  ' 1 ',            )        ;        $arr 2        =   Array (            ' id ' = '  2 ',                ' name ' = '      B '  ,                ' Project '   =  ' 3 ',            ),        );        $arr 3        =   Array (            ' id ' = '  3 ',                ' name ' = '      C '  ,                ' Project '   =  ' 2 ',            ),        );



Ask for help, I have a train of thought, want to let project order, and then loop to determine whether the next Val[project] is the same as the previous, the new array is formed.
But there are other ways to do it.


Reply to discussion (solution)

$arr        =   Array (            ' id ' = ' 1 '  ,                ' name ' = '  a ',                ' project '   =  ' 1 ',            ),            array (                ' id '    = '  2 ',                ' name '      = '  B ',                ' Project '   =  ' 3 ',            ),            array (                ' id '    = '  3 ',                ' name '      = ' =  ') C ',                ' project '   =  ' 2 ',            ),            array (                ' id '  = ' 4 ',                ' name '      = >  ' d ',                ' project '   =  ' 1 ',            )        ; foreach ($arr as $v) $r [' arr '. $v [' project ']][] = $ V;extract ($r);p Rint_r ($arr 1);
Array (    [0] = = Array        (            [id] = 1            [name] + a            [Project] = 1        )    [1] = = Array        (            [id] = 4            [name] + D            [Project] = 1        ))
Other self-authentication

Extract () When do you usually use

Thank you, for the first time know $r[' arr '. $v [' project ']][] = $v; You can also let the array to find the corresponding location, in fact, this sentence to reach the results I want, I kind of way I feel tired

Extract () looked at it before, roughly giving Val directly to the variable in the $+key name.

  • 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.